diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/AFC.c | 703 | ||||
-rw-r--r-- | src/AFC.h | 61 | ||||
-rw-r--r-- | src/MobileSync.c | 55 | ||||
-rw-r--r-- | src/MobileSync.h | 4 | ||||
-rw-r--r-- | src/NotificationProxy.c | 100 | ||||
-rw-r--r-- | src/NotificationProxy.h | 12 | ||||
-rw-r--r-- | src/iphone.c | 37 | ||||
-rw-r--r-- | src/iphone.h | 2 | ||||
-rw-r--r-- | src/lockdown.c | 463 | ||||
-rw-r--r-- | src/lockdown.h | 10 | ||||
-rw-r--r-- | src/userpref.c | 164 | ||||
-rw-r--r-- | src/userpref.h | 22 | ||||
-rw-r--r-- | src/utils.c | 8 | ||||
-rw-r--r-- | src/utils.h | 5 |
14 files changed, 811 insertions, 835 deletions
@@ -21,15 +21,12 @@ #include <stdio.h> #include <stdlib.h> -#include <errno.h> #include <unistd.h> + #include "AFC.h" #include "iphone.h" #include "utils.h" -#include <libiphone/afc.h> - - // This is the maximum size an AFC data packet can be const int MAXIMUM_PACKET_SIZE = (2 << 15); @@ -39,11 +36,7 @@ const int MAXIMUM_PACKET_SIZE = (2 << 15); */ static void afc_lock(afc_client_t client) { - log_debug_msg("Locked\n"); - /*while (client->lock) { - usleep(500); // they say it's obsolete, but whatever - } - client->lock = 1; */ + log_debug_msg("%s: Locked\n", __func__); g_mutex_lock(client->mutex); } @@ -52,9 +45,8 @@ static void afc_lock(afc_client_t client) * @param client The AFC */ static void afc_unlock(afc_client_t client) -{ // just to be pretty - log_debug_msg("Unlocked\n"); - //client->lock = 0; +{ + log_debug_msg("%s: Unlocked\n", __func__); g_mutex_unlock(client->mutex); } @@ -66,30 +58,30 @@ static void afc_unlock(afc_client_t client) * * @return A handle to the newly-connected client or NULL upon error. */ -iphone_error_t afc_new_client(iphone_device_t device, int dst_port, afc_client_t * client) +afc_error_t afc_client_new(iphone_device_t device, int dst_port, afc_client_t * client) { - //makes sure thread environment is available + /* makes sure thread environment is available */ if (!g_thread_supported()) g_thread_init(NULL); if (!device) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; - // Attempt connection + /* attempt connection */ int sfd = usbmuxd_connect(device->handle, dst_port); if (sfd < 0) { - return IPHONE_E_UNKNOWN_ERROR; // ret; + return AFC_E_MUX_ERROR; } afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_int)); client_loc->sfd = sfd; - // Allocate a packet + /* allocate a packet */ client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); if (!client_loc->afc_packet) { usbmuxd_disconnect(client_loc->sfd); free(client_loc); - return IPHONE_E_UNKNOWN_ERROR; + return AFC_E_NO_MEM; } client_loc->afc_packet->packet_num = 0; @@ -101,17 +93,17 @@ iphone_error_t afc_new_client(iphone_device_t device, int dst_port, afc_client_t client_loc->mutex = g_mutex_new(); *client = client_loc; - return IPHONE_E_SUCCESS; + return AFC_E_SUCCESS; } /** Disconnects an AFC client from the phone. * * @param client The client to disconnect. */ -iphone_error_t afc_free_client(afc_client_t client) +afc_error_t afc_client_free(afc_client_t client) { if (!client || client->sfd < 0 || !client->afc_packet) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; usbmuxd_disconnect(client->sfd); free(client->afc_packet); @@ -119,88 +111,7 @@ iphone_error_t afc_free_client(afc_client_t client) g_mutex_free(client->mutex); } free(client); - return IPHONE_E_SUCCESS; -} - -/** - * Returns the AFC error code that has been sent by the device if - * an error occured (set inside receive_AFC_data) - * - * @param client AFC client for that the error value is to be retrieved. - * - * @return AFC error code or -1 on error. - */ -int afc_get_afcerror(afc_client_t client) -{ - int res = -1; - if (client) { - afc_lock(client); - res = client->afcerror; - afc_unlock(client); - } - return res; -} - -/** - * Tries to convert the AFC error value into a meaningful errno value. - * Internally used by afc_get_errno. - * - * @param afcerror AFC error value to convert - * - * @return errno value or -1 if the errno could not be determined. - * - * @see afc_get_errno - */ -static int afcerror_to_errno(int afcerror) -{ - int res = -1; - switch (afcerror) { - case 0: // ERROR_SUCCESS, this means no error. - res = 0; - break; - case 4: // occurs if you try to open a file as directory - res = ENOTDIR; - break; - case 7: // occurs e.g. if you try to close a file handle that - // does not belong to an open file - res = EINVAL; - break; - case 8: // occurs if you try to open a non-existent file - res = ENOENT; - break; - case 9: // occurs if you try to open a directory as file - res = EISDIR; - break; - case 10: // occurs if you try to open a file without permission - res = EPERM; - break; - default: // we'll assume it's an errno value, but report it - log_debug_msg("WARNING: unknown AFC error %d, perhaps it's '%s'?\n", afcerror, strerror(afcerror)); - res = afcerror; - break; - } - - log_debug_msg("Mapped AFC error %d to errno %d: %s\n", afcerror, res, strerror(res)); - - return res; -} - -/** - * Returns the client's AFC error code converted to an errno value. - * - * @param client AFC client for that the errno value is to be retrieved. - * - * @return errno value or -1 on error. - */ -int afc_get_errno(afc_client_t client) -{ - int res = -1; - if (client) { - afc_lock(client); - res = afcerror_to_errno(client->afcerror); - afc_unlock(client); - } - return res; + return AFC_E_SUCCESS; } /** Dispatches an AFC packet over a client. @@ -216,13 +127,14 @@ int afc_get_errno(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(afc_client_t client, const char *data, uint64_t length) +static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t length) { int bytes = 0, offset = 0; char *buffer; if (!client || client->sfd < 0 || !client->afc_packet) return 0; + if (!data || !length) length = 0; @@ -242,12 +154,12 @@ static int dispatch_AFC_packet(afc_client_t client, const char *data, uint64_t l memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); offset = client->afc_packet->this_length - sizeof(AFCPacket); - log_debug_msg("dispatch_AFC_packet: Offset: %i\n", offset); + log_debug_msg("%s: Offset: %i\n", __func__, offset); if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { - log_debug_msg("dispatch_AFC_packet: Length did not resemble what it was supposed"); + log_debug_msg("%s: Length did not resemble what it was supposed", __func__); log_debug_msg("to based on the packet.\n"); - log_debug_msg("length minus offset: %i\n", length - offset); - log_debug_msg("rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); + log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); + log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length); free(buffer); return -1; } @@ -258,19 +170,19 @@ static int dispatch_AFC_packet(afc_client_t client, const char *data, uint64_t l return bytes; } - log_debug_msg("dispatch_AFC_packet: sent the first now go with the second\n"); - log_debug_msg("Length: %i\n", length - offset); - log_debug_msg("Buffer: \n"); + log_debug_msg("%s: sent the first now go with the second\n", __func__); + log_debug_msg("%s: Length: %i\n", __func__, length - offset); + log_debug_msg("%s: Buffer: \n", __func__); log_debug_buffer(data + offset, length - offset); usbmuxd_send(client->sfd, data + offset, length - offset, (uint32_t*)&bytes); return bytes; } else { - log_debug_msg("dispatch_AFC_packet doin things the old way\n"); + log_debug_msg("%s: doin things the old way\n", __func__); buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); - log_debug_msg("dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); + log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); - log_debug_msg("dispatch_AFC_packet packet data follows\n"); + log_debug_msg("%s: packet data follows\n", __func__); if (length > 0) { memcpy(buffer + sizeof(AFCPacket), data, length); } @@ -297,83 +209,81 @@ static int dispatch_AFC_packet(afc_client_t client, const char *data, uint64_t l * received raised a non-trivial error condition (i.e. non-zero with * AFC_ERROR operation) */ -static int receive_AFC_data(afc_client_t client, char **dump_here) +static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *bytes) { AFCPacket header; - int bytes = 0; uint32_t entire_len = 0; uint32_t this_len = 0; uint32_t current_count = 0; uint64_t param1 = -1; - // reset internal afc error value - client->afcerror = 0; + *bytes = 0; - // first, read the AFC header - usbmuxd_recv(client->sfd, (char*)&header, sizeof(AFCPacket), (uint32_t*)&bytes); - if (bytes <= 0) { + /* first, read the AFC header */ + usbmuxd_recv(client->sfd, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); + if (*bytes <= 0) { log_debug_msg("%s: Just didn't get enough.\n", __func__); *dump_here = NULL; - return -1; - } else if ((uint32_t)bytes < sizeof(AFCPacket)) { + return AFC_E_MUX_ERROR; + } else if ((uint32_t)*bytes < sizeof(AFCPacket)) { log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); *dump_here = NULL; - return -1; + return AFC_E_MUX_ERROR; } - // check if it's a valid AFC header + /* check if it's a valid AFC header */ if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN)) { log_debug_msg("%s: Invalid AFC packet received (magic != " AFC_MAGIC ")!\n", __func__); } - // check if it has the correct packet number + /* check if it has the correct packet number */ if (header.packet_num != client->afc_packet->packet_num) { - // otherwise print a warning but do not abort + /* otherwise print a warning but do not abort */ log_debug_msg("%s: ERROR: Unexpected packet number (%lld != %lld) aborting.\n", __func__, header.packet_num, client->afc_packet->packet_num); *dump_here = NULL; - return -1; + return AFC_E_OP_HEADER_INVALID; } - // then, read the attached packet + /* then, read the attached packet */ if (header.this_length < sizeof(AFCPacket)) { log_debug_msg("%s: Invalid AFCPacket header received!\n", __func__); *dump_here = NULL; - return -1; + return AFC_E_OP_HEADER_INVALID; } else if ((header.this_length == header.entire_length) && header.entire_length == sizeof(AFCPacket)) { log_debug_msg("%s: Empty AFCPacket received!\n", __func__); *dump_here = NULL; - if (header.operation == AFC_SUCCESS_RESPONSE) { - return 0; + *bytes = 0; + if (header.operation == AFC_OP_DATA) { + return AFC_E_SUCCESS; } else { - client->afcerror = EIO; - return -1; + return AFC_E_IO_ERROR; } } - log_debug_msg("%s: received AFC packet, full len=%lld, this len=%lld, operation=%lld\n", __func__, header.entire_length, header.this_length, header.operation); + log_debug_msg("%s: received AFC packet, full len=%lld, this len=%lld, operation=0x%llx\n", __func__, header.entire_length, header.this_length, header.operation); entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); this_len = (uint32_t)header.this_length - sizeof(AFCPacket); - // this is here as a check (perhaps a different upper limit is good?) + /* this is here as a check (perhaps a different upper limit is good?) */ if (entire_len > (uint32_t)MAXIMUM_PACKET_SIZE) { fprintf(stderr, "%s: entire_len is larger than MAXIMUM_PACKET_SIZE, (%d > %d)!\n", __func__, entire_len, MAXIMUM_PACKET_SIZE); } *dump_here = (char*)malloc(entire_len); if (this_len > 0) { - usbmuxd_recv(client->sfd, *dump_here, this_len, (uint32_t*)&bytes); - if (bytes <= 0) { + usbmuxd_recv(client->sfd, *dump_here, this_len, (uint32_t*)bytes); + if (*bytes <= 0) { free(*dump_here); *dump_here = NULL; log_debug_msg("%s: Did not get packet contents!\n", __func__); - return -1; - } else if ((uint32_t)bytes < this_len) { + return AFC_E_NOT_ENOUGH_DATA; + } else if ((uint32_t)*bytes < this_len) { free(*dump_here); *dump_here = NULL; log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); - return -1; + return AFC_E_NOT_ENOUGH_DATA; } } @@ -381,12 +291,12 @@ static int receive_AFC_data(afc_client_t client, char **dump_here) if (entire_len > this_len) { while (current_count < entire_len) { - usbmuxd_recv(client->sfd, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)&bytes); - if (bytes <= 0) { - log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, bytes); + usbmuxd_recv(client->sfd, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes); + if (*bytes <= 0) { + log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes); break; } - current_count += bytes; + current_count += *bytes; } if (current_count < entire_len) { log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); @@ -397,39 +307,45 @@ static int receive_AFC_data(afc_client_t client, char **dump_here) param1 = *(uint64_t*)(*dump_here); } - // check for errors - if (header.operation == AFC_SUCCESS_RESPONSE) { - // we got a positive response! - log_debug_msg("%s: got a success response\n", __func__); - } else if (header.operation == AFC_FILE_HANDLE) { - // we got a file handle response - log_debug_msg("%s: got a file handle response, handle=%lld\n", __func__, param1); - } else if (header.operation == AFC_ERROR) { - // error message received - if (param1 == 0) { - // ERROR_SUCCESS, this is not an error! - log_debug_msg("%s: ERROR_SUCCESS\n", __func__); - } else { - // but this is an error! - log_debug_msg("%s: ERROR %lld\n", __func__, param1); + log_debug_msg("%s: packet data size = %i\n", __func__, current_count); + log_debug_msg("%s: packet data follows\n", __func__); + log_debug_buffer(*dump_here, current_count); + + /* check operation types */ + if (header.operation == AFC_OP_STATUS) { + /* status response */ + log_debug_msg("%s: got a status response, code=%lld\n", __func__, param1); + + if (param1 != AFC_E_SUCCESS) { + /* error status */ + /* free buffer */ free(*dump_here); *dump_here = NULL; - // store error value - client->afcerror = (int)param1; - afcerror_to_errno(client->afcerror); - return -1; + return (afc_error_t)param1; } + } else if (header.operation == AFC_OP_DATA) { + /* data response */ + log_debug_msg("%s: got a data response\n", __func__); + } else if (header.operation == AFC_OP_FILE_OPEN_RES) { + /* file handle response */ + log_debug_msg("%s: got a file handle response, handle=%lld\n", __func__, param1); + } else if (header.operation == AFC_OP_FILE_TELL_RES) { + /* tell response */ + log_debug_msg("%s: got a tell response, position=%lld\n", __func__, param1); } else { - // unknown operation code received! + /* unknown operation code received */ free(*dump_here); *dump_here = NULL; + *bytes = 0; log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); - return -1; + return AFC_E_OP_NOT_SUPPORTED; } - return current_count; + + *bytes = current_count; + return AFC_E_SUCCESS; } static int count_nullspaces(char *string, int number) @@ -471,36 +387,34 @@ 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 afc_get_dir_list(afc_client_t client, const char *dir, char ***list) +afc_error_t afc_read_directory(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; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !dir || !list || (list && *list)) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send the command - client->afc_packet->operation = AFC_LIST_DIR; + client->afc_packet->operation = AFC_OP_READ_DIR; client->afc_packet->entire_length = 0; client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, dir, strlen(dir)+1); + bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data - bytes = receive_AFC_data(client, &data); - if (bytes < 0) { + ret = afc_receive_data(client, &data, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } // Parse the data list_loc = make_strings_list(data, bytes); - if (list_loc) - ret = IPHONE_E_SUCCESS; if (data) free(data); @@ -517,29 +431,30 @@ iphone_error_t afc_get_dir_list(afc_client_t client, const char *dir, char ***li * @return A char ** list of parameters as given by AFC or NULL if there was an * error. */ -iphone_error_t afc_get_devinfo(afc_client_t client, char ***infos) +afc_error_t afc_get_device_info(afc_client_t client, char ***infos) { int bytes = 0; char *data = NULL, **list = NULL; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !infos) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send the command - client->afc_packet->operation = AFC_GET_DEVINFO; + client->afc_packet->operation = AFC_OP_GET_DEVINFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, NULL, 0); + bytes = afc_dispatch_packet(client, NULL, 0); if (bytes < 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data - bytes = receive_AFC_data(client, &data); - if (bytes < 0) { + ret = afc_receive_data(client, &data, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } // Parse the data list = make_strings_list(data, bytes); @@ -547,66 +462,67 @@ iphone_error_t afc_get_devinfo(afc_client_t client, char ***infos) free(data); afc_unlock(client); + *infos = list; - return IPHONE_E_SUCCESS; + + return ret; } -/** Deletes a file. +/** Deletes a file or directory. * - * @param client The client to have delete the file. - * @param path The file to delete. (must be a fully-qualified path) + * @param client The client to use. + * @param path The path to delete. (must be a fully-qualified path) * - * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everythong went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_delete_file(afc_client_t client, const char *path) +afc_error_t afc_remove_path(afc_client_t client, const char *path) { char *response = NULL; int bytes; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command client->afc_packet->this_length = client->afc_packet->entire_length = 0; - client->afc_packet->operation = AFC_DELETE; - bytes = dispatch_AFC_packet(client, path, strlen(path)+1); + client->afc_packet->operation = AFC_OP_REMOVE_PATH; + bytes = afc_dispatch_packet(client, path, strlen(path)+1); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &response); + ret = afc_receive_data(client, &response, &bytes); if (response) free(response); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } -/** Renames a file on the phone. +/** Renames a file or directory on the phone. * - * @param client The client to have rename the file. - * @param from The file to rename. (must be a fully-qualified path) - * @param to The new name of the file. (must also be a fully-qualified path) + * @param client The client to have rename. + * @param from The name to rename from. (must be a fully-qualified path) + * @param to The new name. (must also be a fully-qualified path) * - * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everythong went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char *to) +afc_error_t afc_rename_path(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_t))); int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !from || !to || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -614,24 +530,21 @@ iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char 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)+1 + strlen(from)+1); + client->afc_packet->operation = AFC_OP_RENAME_PATH; + bytes = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1); free(send); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &response); + ret = afc_receive_data(client, &response, &bytes); if (response) free(response); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Creates a directory on the phone. @@ -640,38 +553,36 @@ iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char * @param dir The directory's path. (must be a fully-qualified path, I assume * all other mkdir restrictions apply as well) * - * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everythong went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_mkdir(afc_client_t client, const char *dir) +afc_error_t afc_make_directory(afc_client_t client, const char *dir) { int bytes = 0; char *response = NULL; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command - client->afc_packet->operation = AFC_MAKE_DIR; + client->afc_packet->operation = AFC_OP_MAKE_DIR; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = dispatch_AFC_packet(client, dir, strlen(dir)+1); + bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &response); + ret = afc_receive_data(client, &response, &bytes); if (response) free(response); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Gets information about a specific file. @@ -682,38 +593,35 @@ iphone_error_t afc_mkdir(afc_client_t client, const char *dir) * list of strings with the file information. * Set to NULL before calling this function. * - * @return IPHONE_E_SUCCESS on success or an IPHONE_E_* error value + * @return AFC_E_SUCCESS on success or an AFC_E_* error value * when something went wrong. */ -iphone_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) +afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) { char *received = NULL; - int length; + int bytes; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; - if (!client || !path || !infolist) { - return IPHONE_E_INVALID_ARG; - } + if (!client || !path || !infolist) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command - client->afc_packet->operation = AFC_GET_INFO; + client->afc_packet->operation = AFC_OP_GET_FILE_INFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - dispatch_AFC_packet(client, path, strlen(path)+1); + afc_dispatch_packet(client, path, strlen(path)+1); // Receive data - length = receive_AFC_data(client, &received); + ret = afc_receive_data(client, &received, &bytes); if (received) { - *infolist = make_strings_list(received, length); + *infolist = make_strings_list(received, bytes); free(received); - } else { - afc_unlock(client); - return IPHONE_E_AFC_ERROR; } afc_unlock(client); - return IPHONE_E_SUCCESS; + return ret; } /** Opens a file on the phone. @@ -726,21 +634,22 @@ iphone_error_t afc_get_file_info(afc_client_t client, const char *path, char *** * destroying anything previously there. * @param handle Pointer to a uint64_t that will hold the handle of the file * - * @return IPHONE_E_SUCCESS on success or an IPHONE_E_* error on failure. + * @return AFC_E_SUCCESS on success or an AFC_E_* error on failure. */ iphone_error_t -afc_open_file(afc_client_t client, const char *filename, +afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle) { uint32_t ag = 0; - int bytes = 0, length = 0; + int bytes = 0; char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); + afc_error_t ret = AFC_E_UNKNOWN_ERROR; // set handle to 0 so in case an error occurs, the handle is invalid *handle = 0; if (!client || client->sfd < 0|| !client->afc_packet) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -749,34 +658,32 @@ afc_open_file(afc_client_t client, const char *filename, 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->operation = AFC_OP_FILE_OPEN; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, data, 8 + strlen(filename) + 1); + bytes = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1); free(data); if (bytes <= 0) { - log_debug_msg("afc_open_file: Didn't receive a response to the command\n"); + log_debug_msg("%s: Didn't receive a response to the command\n", __func__); afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data - length = receive_AFC_data(client, &data); - if (length > 0 && data) { + ret = afc_receive_data(client, &data, &bytes); + if ((ret == AFC_E_SUCCESS) && (bytes > 0) && data) { afc_unlock(client); // Get the file handle memcpy(handle, data, sizeof(uint64_t)); free(data); - return IPHONE_E_SUCCESS; - } else { - log_debug_msg("afc_open_file: Didn't get any further data\n"); - afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } + log_debug_msg("%s: Didn't get any further data\n", __func__); + afc_unlock(client); - return IPHONE_E_UNKNOWN_ERROR; + return ret; } /** Attempts to the read the given number of bytes from the given file. @@ -789,52 +696,54 @@ afc_open_file(afc_client_t client, const char *filename, * @return The number of bytes read if successful. If there was an error -1. */ iphone_error_t -afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) +afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) { char *input = NULL; int current_count = 0, bytes_loc = 0; const int MAXIMUM_READ_SIZE = 1 << 16; + afc_error_t ret = AFC_E_SUCCESS; if (!client || !client->afc_packet || client->sfd < 0 || handle == 0) - return IPHONE_E_INVALID_ARG; - log_debug_msg("afc_read_file called for length %i\n", length); + return AFC_E_INVALID_ARGUMENT; + log_debug_msg("%s: called for length %i\n", __func__, length); afc_lock(client); // Looping here to get around the maximum amount of data that - // recieve_AFC_data can handle + // afc_receive_data can handle while (current_count < length) { - log_debug_msg("afc_read_file: current count is %i but length is %i\n", current_count, length); + log_debug_msg("%s: current count is %i but length is %i\n", __func__, current_count, length); // Send the read command AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); packet->filehandle = handle; packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; - client->afc_packet->operation = AFC_READ; + client->afc_packet->operation = AFC_OP_READ; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes_loc = dispatch_AFC_packet(client, (char *) packet, sizeof(AFCFilePacket)); + bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); free(packet); if (bytes_loc <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data - bytes_loc = receive_AFC_data(client, &input); - log_debug_msg("afc_read_file: bytes returned: %i\n", bytes_loc); - if (bytes_loc < 0) { + ret = afc_receive_data(client, &input, &bytes_loc); + log_debug_msg("%s: afc_receive_data returned error: %d\n", __func__, ret); + log_debug_msg("%s: bytes returned: %i\n", __func__, bytes_loc); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } else if (bytes_loc == 0) { if (input) free(input); afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; // FIXME check that's actually a - // success + /* FIXME: check that's actually a success */ + return ret; } else { if (input) { - log_debug_msg("afc_read_file: %d\n", bytes_loc); + log_debug_msg("%s: %d\n", __func__, bytes_loc); memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); free(input); input = NULL; @@ -842,11 +751,11 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint } } } - log_debug_msg("afc_read_file: returning current_count as %i\n", current_count); + log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; + return ret; } /** Writes a given number of bytes to a file. @@ -860,7 +769,7 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint * none were written... */ iphone_error_t -afc_write_file(afc_client_t client, uint64_t handle, +afc_file_write(afc_client_t client, uint64_t handle, const char *data, int length, uint32_t * bytes) { char *acknowledgement = NULL; @@ -869,36 +778,37 @@ afc_write_file(afc_client_t client, uint64_t handle, uint32_t segments = (length / MAXIMUM_WRITE_SIZE); int bytes_loc = 0; char *out_buffer = NULL; + afc_error_t ret = AFC_E_SUCCESS; if (!client || !client->afc_packet || client->sfd < 0 || !bytes || (handle == 0)) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); - log_debug_msg("afc_write_file: Write length: %i\n", length); + log_debug_msg("%s: Write length: %i\n", __func__, length); // Divide the file into segments. for (i = 0; i < segments; i++) { // Send the segment 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; + client->afc_packet->operation = AFC_OP_WRITE; out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); memcpy(out_buffer, (char *)&handle, sizeof(uint64_t)); memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); - bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); + bytes_loc = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); if (bytes_loc < 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } free(out_buffer); out_buffer = NULL; current_count += bytes_loc; - bytes_loc = receive_AFC_data(client, &acknowledgement); - if (bytes_loc < 0) { + ret = afc_receive_data(client, &acknowledgement, &bytes_loc); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } else { free(acknowledgement); } @@ -911,16 +821,16 @@ afc_write_file(afc_client_t client, uint64_t handle, if (current_count == (uint32_t)length) { afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; + return ret; } 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; + client->afc_packet->operation = AFC_OP_WRITE; out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); memcpy(out_buffer, (char *) &handle, sizeof(uint64_t)); memcpy(out_buffer + 8, data + current_count, (length - current_count)); - bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); + bytes_loc = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8); free(out_buffer); out_buffer = NULL; @@ -929,19 +839,19 @@ afc_write_file(afc_client_t client, uint64_t handle, if (bytes_loc <= 0) { afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; + return AFC_E_SUCCESS; } zero = bytes_loc; - bytes_loc = receive_AFC_data(client, &acknowledgement); + ret = afc_receive_data(client, &acknowledgement, &bytes_loc); afc_unlock(client); - if (bytes_loc < 0) { - log_debug_msg("afc_write_file: uh oh?\n"); + if (ret != AFC_E_SUCCESS) { + log_debug_msg("%s: uh oh?\n", __func__); } else { free(acknowledgement); } *bytes = current_count; - return IPHONE_E_SUCCESS; + return ret; } /** Closes a file on the phone. @@ -949,95 +859,91 @@ afc_write_file(afc_client_t client, uint64_t handle, * @param client The client to close the file with. * @param handle File handle of a previously opened file. */ -iphone_error_t afc_close_file(afc_client_t client, uint64_t handle) +afc_error_t afc_file_close(afc_client_t client, uint64_t handle) { - if (!client || (handle == 0)) - return IPHONE_E_INVALID_ARG; char *buffer = malloc(sizeof(char) * 8); int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); - log_debug_msg("afc_close_file: File handle %i\n", handle); + log_debug_msg("%s: File handle %i\n", __func__, handle); // Send command memcpy(buffer, &handle, sizeof(uint64_t)); - client->afc_packet->operation = AFC_FILE_CLOSE; + client->afc_packet->operation = AFC_OP_FILE_CLOSE; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 8); + bytes = afc_dispatch_packet(client, buffer, 8); free(buffer); buffer = NULL; - // FIXME: Is this necesary? - // client->afc_packet->entire_length = client->afc_packet->this_length - // = 0; - if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_UNKNOWN_ERROR; + return AFC_E_UNKNOWN_ERROR; } + // Receive the response - bytes = receive_AFC_data(client, &buffer); + ret = afc_receive_data(client, &buffer, &bytes); if (buffer) free(buffer); + afc_unlock(client); - return IPHONE_E_SUCCESS; + + return ret; } /** Locks or unlocks a file on the phone. * - * makes use of flock, see + * makes use of flock on the device, see * http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/flock.2.html * - * operation (same as in sys/file.h on linux): - * - * LOCK_SH 1 // shared lock - * LOCK_EX 2 // exclusive lock - * LOCK_NB 4 // don't block when locking - * LOCK_UN 8 // unlock - * - * @param client The client to close the file with. + * @param client The client to lock the file with. * @param handle File handle of a previously opened file. - * @operation the lock or unlock operation to perform. + * @param operation the lock or unlock operation to perform, this is one of + * AFC_LOCK_SH (shared lock), AFC_LOCK_EX (exclusive lock), + * or AFC_LOCK_UN (unlock). */ -iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, int operation) +afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) { - if (!client || (handle == 0)) - return IPHONE_E_INVALID_ARG; char *buffer = malloc(16); int bytes = 0; uint64_t op = operation; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); - log_debug_msg("afc_lock_file: File handle %i\n", handle); + log_debug_msg("%s: file handle %i\n", __func__, handle); // Send command memcpy(buffer, &handle, sizeof(uint64_t)); memcpy(buffer + 8, &op, 8); - client->afc_packet->operation = AFC_FILE_LOCK; + client->afc_packet->operation = AFC_OP_FILE_LOCK; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 16); + bytes = afc_dispatch_packet(client, buffer, 16); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); - log_debug_msg("fuck\n"); - return IPHONE_E_UNKNOWN_ERROR; + log_debug_msg("%s: could not send lock command\n", __func__); + return AFC_E_UNKNOWN_ERROR; } // Receive the response - bytes = receive_AFC_data(client, &buffer); + ret = afc_receive_data(client, &buffer, &bytes); if (buffer) { log_debug_buffer(buffer, bytes); free(buffer); } afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + + return ret; } /** Seeks to a given position of a pre-opened file on the phone. @@ -1047,13 +953,17 @@ iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, int operation * @param offset Seek offset. * @param whence Seeking direction, one of SEEK_SET, SEEK_CUR, or SEEK_END. * - * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure. + * @return AFC_E_SUCCESS on success, AFC_E_NOT_ENOUGH_DATA on failure. */ -iphone_error_t afc_seek_file(afc_client_t client, uint64_t handle, int64_t offset, int whence) +afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) { char *buffer = (char *) malloc(sizeof(char) * 24); uint32_t zero = 0; int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1062,27 +972,70 @@ iphone_error_t afc_seek_file(afc_client_t client, uint64_t handle, int64_t offse memcpy(buffer + 8, &whence, sizeof(int32_t)); // fromwhere memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset - client->afc_packet->operation = AFC_FILE_SEEK; + client->afc_packet->operation = AFC_OP_FILE_SEEK; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 24); + bytes = afc_dispatch_packet(client, buffer, 24); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &buffer); + ret = afc_receive_data(client, &buffer, &bytes); if (buffer) free(buffer); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; + return ret; +} + +/** Returns current position in a pre-opened file on the phone. + * + * @param client The client to use. + * @param handle File handle of a previously opened file. + * @param position Position in bytes of indicator + * + * @return AFC_E_SUCCESS on success, AFC_E_NOT_ENOUGH_DATA on failure. + */ +afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) +{ + char *buffer = (char *) malloc(sizeof(char) * 8); + int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; + + afc_lock(client); + + // Send the command + memcpy(buffer, &handle, sizeof(uint64_t)); // handle + client->afc_packet->operation = AFC_OP_FILE_TELL; + client->afc_packet->this_length = client->afc_packet->entire_length = 0; + bytes = afc_dispatch_packet(client, buffer, 8); + free(buffer); + buffer = NULL; + + if (bytes <= 0) { + afc_unlock(client); + return AFC_E_NOT_ENOUGH_DATA; } - return IPHONE_E_SUCCESS; + + // Receive the data + ret = afc_receive_data(client, &buffer, &bytes); + if (bytes > 0 && buffer) { + /* Get the position */ + memcpy(position, buffer, sizeof(uint64_t)); + } + if (buffer) + free(buffer); + + afc_unlock(client); + + return ret; } /** Sets the size of a file on the phone. @@ -1096,37 +1049,38 @@ iphone_error_t afc_seek_file(afc_client_t client, uint64_t handle, int64_t offse * @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 afc_truncate_file(afc_client_t client, uint64_t handle, uint64_t newsize) +afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) { char *buffer = (char *) malloc(sizeof(char) * 16); int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command memcpy(buffer, &handle, sizeof(uint64_t)); // handle memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize - client->afc_packet->operation = AFC_FILE_TRUNCATE; + client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 16); + bytes = afc_dispatch_packet(client, buffer, 16); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &buffer); + ret = afc_receive_data(client, &buffer, &bytes); if (buffer) free(buffer); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Sets the size of a file on the phone without prior opening it. @@ -1135,18 +1089,19 @@ iphone_error_t afc_truncate_file(afc_client_t client, uint64_t handle, uint64_t * @param path The path of the file to be truncated. * @param newsize The size to set the file to. * - * @return IPHONE_E_SUCCESS if everything went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) +afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); int bytes = 0; uint64_t size_requested = newsize; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1154,24 +1109,21 @@ iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize memcpy(send, &size_requested, 8); memcpy(send + 8, path, strlen(path) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; - client->afc_packet->operation = AFC_TRUNCATE; - bytes = dispatch_AFC_packet(client, send, 8 + strlen(path) + 1); + client->afc_packet->operation = AFC_OP_TRUNCATE; + bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); free(send); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &response); + ret = afc_receive_data(client, &response, &bytes); if (response) free(response); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Creates a hard link or symbolic link on the device. @@ -1181,47 +1133,44 @@ iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize * @param target The file to be linked. * @param linkname The name of link. * - * @return IPHONE_E_SUCCESS if everything went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname) +afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); int bytes = 0; uint64_t type = linktype; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !target || !linkname || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); - log_debug_msg("link type: %lld\n", type); - log_debug_msg("target: %s, length:%d\n", target, strlen(target)); - log_debug_msg("linkname: %s, length:%d\n", linkname, strlen(linkname)); + log_debug_msg("%s: link type: %lld\n", __func__, type); + log_debug_msg("%s: target: %s, length:%d\n", __func__, target, strlen(target)); + log_debug_msg("%s: linkname: %s, length:%d\n", __func__, linkname, strlen(linkname)); // Send command memcpy(send, &type, 8); memcpy(send + 8, target, strlen(target) + 1); memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; - client->afc_packet->operation = AFC_MAKE_LINK; - bytes = dispatch_AFC_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); + client->afc_packet->operation = AFC_OP_MAKE_LINK; + bytes = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); free(send); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response - bytes = receive_AFC_data(client, &response); + ret = afc_receive_data(client, &response, &bytes); if (response) free(response); afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_NOT_ENOUGH_DATA; - } else { - return IPHONE_E_SUCCESS; - } + return ret; } @@ -26,6 +26,8 @@ #include <glib.h> #include <stdint.h> +#include "libiphone/afc.h" + #define AFC_MAGIC "CFA6LPAA" #define AFC_MAGIC_LEN (8) @@ -48,39 +50,38 @@ struct afc_client_int { AFCPacket *afc_packet; int file_handle; int lock; - int afcerror; GMutex *mutex; }; +/* AFC Operations */ enum { - AFC_ERROR = 0x00000001, - AFC_SUCCESS_RESPONSE = 0x00000002, - AFC_LIST_DIR = 0x00000003, // ReadDir - // 0x00000004 // ReadFile - // 0x00000005 // WriteFile - // 0x00000006 // WritePart - AFC_TRUNCATE = 0x00000007, // Truncate - AFC_DELETE = 0x00000008, // RemovePath - AFC_MAKE_DIR = 0x00000009, // MakeDir - AFC_GET_INFO = 0x0000000a, // GetFileInfo - AFC_GET_DEVINFO = 0x0000000b, // GetDeviceInfo - // 0x0000000c // same as 5, but writes to temp file, then renames it. - AFC_FILE_OPEN = 0x0000000d, // FileRefOpen - AFC_FILE_HANDLE = 0x0000000e, // _unknownPacket - AFC_READ = 0x0000000f, // FileRefRead - AFC_WRITE = 0x00000010, // FileRefWrite - AFC_FILE_SEEK = 0x00000011, // FileRefSeek - AFC_FILE_TELL = 0x00000012, // FileRefTell - // 0x00000013 // _unknownPacket - AFC_FILE_CLOSE = 0x00000014, // FileRefClose - AFC_FILE_TRUNCATE = 0x00000015, // FileRefSetFileSize (ftruncate) - // 0x00000016 // SetFatalError - // 0x00000017 // SetConnectionOptions - AFC_RENAME = 0x00000018, // RenamePath - // 0x00000019 // SetFSBlockSize (0x800000) - // 0x0000001A // SetBlockSize (0x800000) - AFC_FILE_LOCK = 0x0000001B, // FileRefLock - AFC_MAKE_LINK = 0x0000001C // MakeLink + AFC_OP_STATUS = 0x00000001, // Status + AFC_OP_DATA = 0x00000002, // Data + AFC_OP_READ_DIR = 0x00000003, // ReadDir + AFC_OP_READ_FILE = 0x00000004, // ReadFile + AFC_OP_WRITE_FILE = 0x00000005, // WriteFile + AFC_OP_WRITE_PART = 0x00000006, // WritePart + AFC_OP_TRUNCATE = 0x00000007, // TruncateFile + AFC_OP_REMOVE_PATH = 0x00000008, // RemovePath + AFC_OP_MAKE_DIR = 0x00000009, // MakeDir + AFC_OP_GET_FILE_INFO = 0x0000000a, // GetFileInfo + AFC_OP_GET_DEVINFO = 0x0000000b, // GetDeviceInfo + AFC_OP_WRITE_FILE_ATOM = 0x0000000c, // WriteFileAtomic (tmp file+rename) + AFC_OP_FILE_OPEN = 0x0000000d, // FileRefOpen + AFC_OP_FILE_OPEN_RES = 0x0000000e, // FileRefOpenResult + AFC_OP_READ = 0x0000000f, // FileRefRead + AFC_OP_WRITE = 0x00000010, // FileRefWrite + AFC_OP_FILE_SEEK = 0x00000011, // FileRefSeek + AFC_OP_FILE_TELL = 0x00000012, // FileRefTell + AFC_OP_FILE_TELL_RES = 0x00000013, // FileRefTellResult + AFC_OP_FILE_CLOSE = 0x00000014, // FileRefClose + AFC_OP_FILE_SET_SIZE = 0x00000015, // FileRefSetFileSize (ftruncate) + AFC_OP_GET_CON_INFO = 0x00000016, // GetConnectionInfo + AFC_OP_SET_CON_OPTIONS = 0x00000017, // SetConnectionOptions + AFC_OP_RENAME_PATH = 0x00000018, // RenamePath + AFC_OP_SET_FS_BS = 0x00000019, // SetFSBlockSize (0x800000) + AFC_OP_SET_SOCKET_BS = 0x0000001A, // SetSocketBlockSize (0x800000) + AFC_OP_FILE_LOCK = 0x0000001B, // FileRefLock + AFC_OP_MAKE_LINK = 0x0000001C // MakeLink }; -static int afcerror_to_errno(int afcerror); diff --git a/src/MobileSync.c b/src/MobileSync.c index b9a1cb0..4463251 100644 --- a/src/MobileSync.c +++ b/src/MobileSync.c @@ -19,25 +19,27 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "MobileSync.h" #include <plist/plist.h> #include <string.h> #include <stdlib.h> #include <arpa/inet.h> +#include "MobileSync.h" +#include "iphone.h" +#include "utils.h" #define MSYNC_VERSION_INT1 100 #define MSYNC_VERSION_INT2 100 -iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, +mobilesync_error_t mobilesync_client_new(iphone_device_t device, int dst_port, mobilesync_client_t * client) { if (!device || dst_port == 0 || !client || *client) - return IPHONE_E_INVALID_ARG; + return MOBILESYNC_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; - // Attempt connection + /* Attempt connection */ int sfd = usbmuxd_connect(device->handle, dst_port); if (sfd < 0) { return ret; @@ -46,10 +48,10 @@ iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int)); client_loc->sfd = sfd; - //perform handshake + /* perform handshake */ plist_t array = NULL; - //first receive version + /* first receive version */ ret = mobilesync_recv(client_loc, &array); plist_t msg_node = plist_find_node_by_string(array, "DLMessageVersionExchange"); @@ -86,17 +88,20 @@ iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, plist_t rep_node = plist_find_node_by_string(array, "DLMessageDeviceReady"); if (rep_node) { - ret = IPHONE_E_SUCCESS; + ret = MOBILESYNC_E_SUCCESS; *client = client_loc; } + else + { + ret = MOBILESYNC_E_BAD_VERSION; + } plist_free(array); array = NULL; - } } - if (IPHONE_E_SUCCESS != ret) - mobilesync_free_client(client_loc); + if (MOBILESYNC_E_SUCCESS != ret) + mobilesync_client_free(client_loc); return ret; } @@ -115,13 +120,13 @@ static void mobilesync_disconnect(mobilesync_client_t client) array = NULL; } -iphone_error_t mobilesync_free_client(mobilesync_client_t client) +mobilesync_error_t mobilesync_client_free(mobilesync_client_t client) { if (!client) return IPHONE_E_INVALID_ARG; mobilesync_disconnect(client); - return usbmuxd_disconnect(client->sfd); + return (usbmuxd_disconnect(client->sfd) == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR); } /** Polls the iPhone for MobileSync data. @@ -131,11 +136,11 @@ iphone_error_t mobilesync_free_client(mobilesync_client_t client) * * @return an error code */ -iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) +mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) { if (!client || !plist || (plist && *plist)) - return IPHONE_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + return MOBILESYNC_E_INVALID_ARG; + mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; char *receive = NULL; uint32_t datalen = 0, bytes = 0, received_bytes = 0; @@ -145,14 +150,14 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) receive = (char *) malloc(sizeof(char) * datalen); /* fill buffer and request more packets if needed */ - while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) { + while ((received_bytes < datalen) && (ret == MOBILESYNC_E_SUCCESS)) { ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes); received_bytes += bytes; } - if (ret != IPHONE_E_SUCCESS) { + if (ret != MOBILESYNC_E_SUCCESS) { free(receive); - return ret; + return MOBILESYNC_E_MUX_ERROR; } plist_from_bin(receive, received_bytes, plist); @@ -161,7 +166,7 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) char *XMLContent = NULL; uint32_t length = 0; plist_to_xml(*plist, &XMLContent, &length); - log_dbg_msg(DBGMASK_MOBILESYNC, "Recv msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); + log_dbg_msg(DBGMASK_MOBILESYNC, "%s: plist size: %i\nbuffer :\n%s\n", __func__, length, XMLContent); free(XMLContent); return ret; @@ -177,15 +182,15 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) * * @return an error code */ -iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) +mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) { if (!client || !plist) - return IPHONE_E_INVALID_ARG; + return MOBILESYNC_E_INVALID_ARG; char *XMLContent = NULL; uint32_t length = 0; plist_to_xml(plist, &XMLContent, &length); - log_dbg_msg(DBGMASK_MOBILESYNC, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); + log_dbg_msg(DBGMASK_MOBILESYNC, "%s: plist size: %i\nbuffer :\n%s\n", __func__, length, XMLContent); free(XMLContent); char *content = NULL; @@ -195,7 +200,7 @@ iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) char *real_query; int bytes; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; real_query = (char *) malloc(sizeof(char) * (length + 4)); length = htonl(length); @@ -204,6 +209,6 @@ iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes); free(real_query); - return ret; + return (ret == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR); } diff --git a/src/MobileSync.h b/src/MobileSync.h index 5279ce0..6347399 100644 --- a/src/MobileSync.h +++ b/src/MobileSync.h @@ -21,12 +21,8 @@ #ifndef MOBILESYNC_H #define MOBILESYNC_H -#include "iphone.h" -#include "utils.h" #include "libiphone/mobilesync.h" -#include <plist/plist.h> - struct mobilesync_client_int { int sfd; }; diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c index 511b07f..e4735cc 100644 --- a/src/NotificationProxy.c +++ b/src/NotificationProxy.c @@ -25,6 +25,7 @@ #include <unistd.h> #include <arpa/inet.h> #include <plist/plist.h> + #include "NotificationProxy.h" #include "iphone.h" #include "utils.h" @@ -61,24 +62,24 @@ static void np_unlock(np_client_t client) * @param client NP to send data to * @param dict plist to send * - * @return IPHONE_E_SUCCESS or an error code. + * @return NP_E_SUCCESS or an error code. */ -static iphone_error_t np_plist_send(np_client_t client, plist_t dict) +static np_error_t np_plist_send(np_client_t client, plist_t dict) { char *XML_content = NULL; uint32_t length = 0; uint32_t nlen = 0; int bytes = 0; - iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + np_error_t res = NP_E_UNKNOWN_ERROR; if (!client || !dict) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } plist_to_xml(dict, &XML_content, &length); if (!XML_content || length == 0) { - return IPHONE_E_PLIST_ERROR; + return NP_E_PLIST_ERROR; } nlen = htonl(length); @@ -87,7 +88,7 @@ static iphone_error_t np_plist_send(np_client_t client, plist_t dict) usbmuxd_send(client->sfd, XML_content, length, (uint32_t*)&bytes); if (bytes > 0) { if ((uint32_t)bytes == length) { - res = IPHONE_E_SUCCESS; + res = NP_E_SUCCESS; } else { log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length); } @@ -110,19 +111,19 @@ static iphone_error_t np_plist_send(np_client_t client, plist_t dict) * * @return A handle to the newly-connected client or NULL upon error. */ -iphone_error_t np_new_client ( iphone_device_t device, int dst_port, np_client_t *client ) +np_error_t np_client_new(iphone_device_t device, int dst_port, np_client_t *client) { - //makes sure thread environment is available + /* makes sure thread environment is available */ if (!g_thread_supported()) g_thread_init(NULL); if (!device) - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; - // Attempt connection + /* Attempt connection */ int sfd = usbmuxd_connect(device->handle, dst_port); if (sfd < 0) { - return IPHONE_E_UNKNOWN_ERROR; //ret; + return NP_E_UNKNOWN_ERROR; } np_client_t client_loc = (np_client_t) malloc(sizeof(struct np_client_int)); @@ -133,17 +134,17 @@ iphone_error_t np_new_client ( iphone_device_t device, int dst_port, np_client_t client_loc->notifier = NULL; *client = client_loc; - return IPHONE_E_SUCCESS; + return NP_E_SUCCESS; } /** Disconnects an NP client from the phone. * * @param client The client to disconnect. */ -iphone_error_t np_free_client ( np_client_t client ) +np_error_t np_client_free(np_client_t client) { if (!client) - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; usbmuxd_disconnect(client->sfd); client->sfd = -1; @@ -156,7 +157,7 @@ iphone_error_t np_free_client ( np_client_t client ) } free(client); - return IPHONE_E_SUCCESS; + return NP_E_SUCCESS; } /** Sends a notification to the device's Notification Proxy. @@ -168,10 +169,10 @@ iphone_error_t np_free_client ( np_client_t client ) * @param client The client to send to * @param notification The notification message to send */ -iphone_error_t np_post_notification( np_client_t client, const char *notification ) +np_error_t np_post_notification(np_client_t client, const char *notification) { if (!client || !notification) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } np_lock(client); @@ -181,7 +182,7 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio plist_add_sub_key_el(dict, "Name"); plist_add_sub_string_el(dict, notification); - iphone_error_t res = np_plist_send(client, dict); + np_error_t res = np_plist_send(client, dict); plist_free(dict); dict = plist_new_dict(); @@ -191,7 +192,7 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio res = np_plist_send(client, dict); plist_free(dict); - if (res != IPHONE_E_SUCCESS) { + if (res != NP_E_SUCCESS) { log_debug_msg("%s: Error sending XML plist to device!\n", __func__); } @@ -204,10 +205,10 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio * @param client The client to send to * @param notification The notifications that should be observed. */ -iphone_error_t np_observe_notification( np_client_t client, const char *notification ) +np_error_t np_observe_notification( np_client_t client, const char *notification ) { if (!client || !notification) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } np_lock(client); @@ -217,8 +218,8 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica plist_add_sub_key_el(dict, "Name"); plist_add_sub_string_el(dict, notification); - iphone_error_t res = np_plist_send(client, dict); - if (res != IPHONE_E_SUCCESS) { + np_error_t res = np_plist_send(client, dict); + if (res != NP_E_SUCCESS) { log_debug_msg("%s: Error sending XML plist to device!\n", __func__); } plist_free(dict); @@ -227,7 +228,6 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica return res; } - /** Notifies the iphone to send a notification on specified events. * * observation messages seen so far: @@ -247,14 +247,14 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica * terminating NULL entry. However this parameter can be NULL; in this case, * the default set of notifications will be used. */ -iphone_error_t np_observe_notifications( np_client_t client, const char **notification_spec ) +np_error_t np_observe_notifications(np_client_t client, const char **notification_spec) { int i = 0; - iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + np_error_t res = NP_E_UNKNOWN_ERROR; const char **notifications = notification_spec; if (!client) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } if (!notifications) { @@ -263,7 +263,7 @@ iphone_error_t np_observe_notifications( np_client_t client, const char **notifi while (notifications[i]) { res = np_observe_notification(client, notifications[i]); - if (res != IPHONE_E_SUCCESS) { + if (res != NP_E_SUCCESS) { break; } i++; @@ -279,24 +279,22 @@ iphone_error_t np_observe_notifications( np_client_t client, const char **notifi * @param notification Pointer to a buffer that will be allocated and filled * with the notification that has been received. * - * @return IPHONE_E_SUCCESS if a notification has been received, - * IPHONE_E_TIMEOUT if nothing has been received, + * @return 0 if a notification has been received or nothing has been received, * or an error value if an error occured. * * @note You probably want to check out np_set_notify_callback * @see np_set_notify_callback */ -static iphone_error_t np_get_notification( np_client_t client, char **notification ) +static int np_get_notification(np_client_t client, char **notification) { uint32_t bytes = 0; - iphone_error_t res; + int res = 0; uint32_t pktlen = 0; char *XML_content = NULL; plist_t dict = NULL; - if (!client || client->sfd < 0 || *notification) { - return IPHONE_E_INVALID_ARG; - } + if (!client || client->sfd < 0 || *notification) + return -1; np_lock(client); @@ -304,7 +302,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati log_debug_msg("NotificationProxy: initial read=%i\n", bytes); if (bytes < 4) { log_debug_msg("NotificationProxy: no notification received!\n"); - res = IPHONE_E_TIMEOUT; + res = 0; } else { if ((char)pktlen == 0) { pktlen = ntohl(pktlen); @@ -314,7 +312,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati usbmuxd_recv_timeout(client->sfd, XML_content, pktlen, &bytes, 1000); if (bytes <= 0) { - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } else { log_debug_msg("NotificationProxy: received data:\n"); log_debug_buffer(XML_content, pktlen); @@ -322,7 +320,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati plist_from_xml(XML_content, bytes, &dict); if (!dict) { np_unlock(client); - return IPHONE_E_PLIST_ERROR; + return -2; } plist_t cmd_key_node = plist_find_node_by_key(dict, "Command"); @@ -347,21 +345,21 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati plist_get_string_val(name_value_node, &name_value); } - res = IPHONE_E_PLIST_ERROR; + res = -2; if (name_key && name_value && !strcmp(name_key, "Name")) { *notification = name_value; log_debug_msg("%s: got notification %s\n", __func__, name_value); - res = IPHONE_E_SUCCESS; + res = 0; } free(name_key); } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) { log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__); - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } else if (cmd_value) { log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__); - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } else { - res = IPHONE_E_PLIST_ERROR; + res = -2; } if (cmd_value) { free(cmd_value); @@ -372,7 +370,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati XML_content = NULL; } } else { - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } } @@ -418,15 +416,15 @@ gpointer np_notifier( gpointer arg ) * @param notify_cb pointer to a callback function or NULL to de-register a * previously set callback function * - * @return IPHONE_E_SUCCESS when the callback was successfully registered, + * @return NP_E_SUCCESS when the callback was successfully registered, * or an error value when an error occured. */ -iphone_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb ) +np_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb ) { - if (!client) { - return IPHONE_E_INVALID_ARG; - } - iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + if (!client) + return NP_E_INVALID_ARG; + + np_error_t res = NP_E_UNKNOWN_ERROR; np_lock(client); if (client->notifier) { @@ -446,7 +444,7 @@ iphone_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify client->notifier = g_thread_create(np_notifier, npt, TRUE, NULL); if (client->notifier) { - res = IPHONE_E_SUCCESS; + res = NP_E_SUCCESS; } } } else { diff --git a/src/NotificationProxy.h b/src/NotificationProxy.h index a10cde4..bc5be43 100644 --- a/src/NotificationProxy.h +++ b/src/NotificationProxy.h @@ -18,17 +18,20 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libiphone/notification_proxy.h" +#ifndef INOTIFICATION_PROXY_H +#define INOTIFICATION_PROXY_H #include <glib.h> +#include "libiphone/notification_proxy.h" + struct np_client_int { int sfd; GMutex *mutex; GThread *notifier; }; -static const char *np_default_notifications[10] = { +static const char *np_default_notifications[11] = { NP_SYNC_SUSPEND_REQUEST, NP_SYNC_RESUME_REQUEST, NP_PHONE_NUMBER_CHANGED, @@ -38,7 +41,10 @@ static const char *np_default_notifications[10] = { NP_DS_DOMAIN_CHANGED, NP_APP_INSTALLED, NP_APP_UNINSTALLED, + NP_ITDBPREP_DID_END, NULL }; -gpointer np_notifier( gpointer arg ); +gpointer np_notifier(gpointer arg); + +#endif diff --git a/src/iphone.c b/src/iphone.c index 0e179e7..e694373 100644 --- a/src/iphone.c +++ b/src/iphone.c @@ -19,13 +19,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "iphone.h" -#include "utils.h" #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <errno.h> -#include <libiphone/libiphone.h> + +#include "iphone.h" +#include "utils.h" /** * Retrieves a list of connected devices from usbmuxd and matches their @@ -52,11 +51,11 @@ iphone_error_t iphone_get_device_by_uuid(iphone_device_t * device, const char *u } if (dev_list && dev_list[0].handle > 0) { if (!uuid) { - // select first device found if no UUID specified + /* select first device found if no UUID specified */ handle = dev_list[0].handle; strcpy(serial_number, dev_list[0].serial_number); } else { - // otherwise walk through the list + /* otherwise walk through the list */ for (i = 0; dev_list[i].handle > 0; i++) { log_debug_msg("%s: device handle=%d, uuid=%s\n", __func__, dev_list[i].handle, dev_list[i].serial_number); if (strcasecmp(uuid, dev_list[i].serial_number) == 0) { @@ -95,22 +94,22 @@ iphone_error_t iphone_get_device(iphone_device_t * device) return iphone_get_device_by_uuid(device, NULL); } -uint32_t iphone_get_device_handle(iphone_device_t device) +iphone_error_t iphone_device_get_handle(iphone_device_t device, uint32_t *handle) { - if (device) { - return device->handle; - } else { - return 0; - } + if (!device) + return IPHONE_E_INVALID_ARG; + + *handle = device->handle; + return IPHONE_E_SUCCESS; } -char* iphone_get_uuid(iphone_device_t device) +iphone_error_t iphone_device_get_uuid(iphone_device_t device, char **uuid) { - if (device) { - return device->serial_number; - } else { - return NULL; - } + if (!device) + return IPHONE_E_INVALID_ARG; + + *uuid = strdup(device->serial_number); + return IPHONE_E_SUCCESS; } /** Cleans up an iPhone structure, then frees the structure itself. @@ -119,7 +118,7 @@ char* iphone_get_uuid(iphone_device_t device) * * @param phone A pointer to an iPhone structure. */ -iphone_error_t iphone_free_device(iphone_device_t device) +iphone_error_t iphone_device_free(iphone_device_t device) { if (!device) return IPHONE_E_INVALID_ARG; diff --git a/src/iphone.h b/src/iphone.h index 2ed0fba..6e14280 100644 --- a/src/iphone.h +++ b/src/iphone.h @@ -24,6 +24,8 @@ #include <stdint.h> +#include "libiphone/libiphone.h" + struct iphone_device_int { char *buffer; uint32_t handle; diff --git a/src/lockdown.c b/src/lockdown.c index 1a434aa..bc430c9 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -19,10 +19,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "utils.h" -#include "iphone.h" -#include "lockdown.h" -#include "userpref.h" #include <arpa/inet.h> #include <errno.h> #include <string.h> @@ -30,9 +26,13 @@ #include <glib.h> #include <libtasn1.h> #include <gnutls/x509.h> - #include <plist/plist.h> +#include "lockdown.h" +#include "iphone.h" +#include "utils.h" +#include "userpref.h" + #define RESULT_SUCCESS 0 #define RESULT_FAILURE 1 @@ -126,21 +126,23 @@ static int lockdown_check_result(plist_t dict, const char *query_match) * the StopSession Request to the device. * * @param control The lockdown client + * + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_stop_session(lockdownd_client_t client) +lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id) { if (!client) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); plist_add_sub_key_el(dict, "Request"); plist_add_sub_string_el(dict, "StopSession"); plist_add_sub_key_el(dict, "SessionID"); - plist_add_sub_string_el(dict, client->session_id); + plist_add_sub_string_el(dict, session_id); - log_dbg_msg(DBGMASK_LOCKDOWND, "iphone_lckd_stop_session() called\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__); ret = lockdownd_send(client, dict); @@ -150,14 +152,14 @@ iphone_error_t lockdownd_stop_session(lockdownd_client_t client) ret = lockdownd_recv(client, &dict); if (!dict) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n"); - return IPHONE_E_PLIST_ERROR; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: LOCKDOWN_E_PLIST_ERROR\n", __func__); + return LOCKDOWN_E_PLIST_ERROR; } - ret = IPHONE_E_UNKNOWN_ERROR; + ret = LOCKDOWN_E_UNKNOWN_ERROR; if (lockdown_check_result(dict, "StopSession") == RESULT_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): success\n"); - ret = IPHONE_E_SUCCESS; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); + ret = LOCKDOWN_E_SUCCESS; } plist_free(dict); dict = NULL; @@ -171,19 +173,21 @@ iphone_error_t lockdownd_stop_session(lockdownd_client_t client) * performing a close notify, which is done by "gnutls_bye". * * @param client The lockdown client + * + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -static iphone_error_t lockdownd_stop_ssl_session(lockdownd_client_t client) +static lockdownd_error_t lockdownd_stop_ssl_session(lockdownd_client_t client) { if (!client) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_ssl_session(): invalid argument!\n"); - return IPHONE_E_INVALID_ARG; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: invalid argument!\n", __func__); + return LOCKDOWN_E_INVALID_ARG; } - iphone_error_t ret = IPHONE_E_SUCCESS; + lockdownd_error_t ret = LOCKDOWN_E_SUCCESS; if (client->in_SSL) { - log_dbg_msg(DBGMASK_LOCKDOWND, "Stopping SSL Session\n"); - ret = lockdownd_stop_session(client); - log_dbg_msg(DBGMASK_LOCKDOWND, "Sending SSL close notify\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping SSL session\n", __func__); + ret = lockdownd_stop_session(client, client->session_id); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending SSL close notify\n", __func__); gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); } if (client->ssl_session) { @@ -198,12 +202,14 @@ static iphone_error_t lockdownd_stop_ssl_session(lockdownd_client_t client) /** Closes the lockdownd client and does the necessary housekeeping. * * @param client The lockdown client + * + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_free_client(lockdownd_client_t client) +lockdownd_error_t lockdownd_client_free(lockdownd_client_t client) { if (!client) - return IPHONE_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + return LOCKDOWN_E_INVALID_ARG; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; lockdownd_stop_ssl_session(client); @@ -224,13 +230,13 @@ iphone_error_t lockdownd_free_client(lockdownd_client_t client) * @param control The lockdownd client * @param plist The plist to store the received data * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist) +lockdownd_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist) { if (!client || !plist || (plist && *plist)) - return IPHONE_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + return LOCKDOWN_E_INVALID_ARG; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; char *receive = NULL; uint32_t datalen = 0, bytes = 0, received_bytes = 0; @@ -240,10 +246,10 @@ iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist) ssize_t res = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen)); if (res < 0) { log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_recv: Error occured: %s\n", gnutls_strerror(res)); - return IPHONE_E_SSL_ERROR; + return LOCKDOWN_E_SSL_ERROR; } else { bytes = res; - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } } datalen = ntohl(datalen); @@ -253,40 +259,40 @@ iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist) /* fill buffer and request more packets if needed */ if (!client->in_SSL) { - while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) { + while ((received_bytes < datalen) && (ret == LOCKDOWN_E_SUCCESS)) { ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes); received_bytes += bytes; } } else { ssize_t res = 0; - while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) { + while ((received_bytes < datalen) && (ret == LOCKDOWN_E_SUCCESS)) { res = gnutls_record_recv(*client->ssl_session, receive + received_bytes, datalen - received_bytes); if (res < 0) { log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_recv: Error occured: %s\n", gnutls_strerror(res)); - ret = IPHONE_E_SSL_ERROR; + ret = LOCKDOWN_E_SSL_ERROR; } else { received_bytes += res; - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } } } - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { free(receive); return ret; } if ((ssize_t)received_bytes <= 0) { free(receive); - return IPHONE_E_NOT_ENOUGH_DATA; + return LOCKDOWN_E_NOT_ENOUGH_DATA; } - log_dbg_msg(DBGMASK_LOCKDOWND, "Recv msg :\nsize : %i\nbuffer :\n%s\n", received_bytes, receive); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: received msg size: %i, buffer follows:\n%s", __func__, received_bytes, receive); plist_from_xml(receive, received_bytes, plist); free(receive); if (!*plist) - ret = IPHONE_E_PLIST_ERROR; + ret = LOCKDOWN_E_PLIST_ERROR; return ret; } @@ -299,27 +305,27 @@ iphone_error_t lockdownd_recv(lockdownd_client_t client, plist_t *plist) * @param client The lockdownd client * @param plist The plist to send * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_send(lockdownd_client_t client, plist_t plist) +lockdownd_error_t lockdownd_send(lockdownd_client_t client, plist_t plist) { if (!client || !plist) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; char *real_query; int bytes; char *XMLContent = NULL; uint32_t length = 0; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_to_xml(plist, &XMLContent, &length); - log_dbg_msg(DBGMASK_LOCKDOWND, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending msg size %i, buffer follows:\n%s", __func__, length, XMLContent); real_query = (char *) malloc(sizeof(char) * (length + 4)); length = htonl(length); memcpy(real_query, &length, sizeof(length)); memcpy(real_query + 4, XMLContent, ntohl(length)); free(XMLContent); - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): made the query, sending it along\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: made the query, sending it along\n", __func__); if (!client->in_SSL) ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes); @@ -327,16 +333,16 @@ iphone_error_t lockdownd_send(lockdownd_client_t client, plist_t plist) ssize_t res = gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); if (res < 0) { log_dbg_msg(DBGMASK_LOCKDOWND, "gnutls_record_send: Error occured: %s\n", gnutls_strerror(res)); - ret = IPHONE_E_SSL_ERROR; + ret = LOCKDOWN_E_SSL_ERROR; } else { bytes = res; - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } } - if (ret == IPHONE_E_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sent it!\n"); + if (ret == LOCKDOWN_E_SUCCESS) { + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sent it!\n", __func__); } else { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sending failed!\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending failed!\n", __func__); } free(real_query); @@ -347,20 +353,20 @@ iphone_error_t lockdownd_send(lockdownd_client_t client, plist_t plist) * * @param client The lockdownd client * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_query_type(lockdownd_client_t client) +lockdownd_error_t lockdownd_query_type(lockdownd_client_t client) { if (!client) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); plist_add_sub_key_el(dict, "Request"); plist_add_sub_string_el(dict, "QueryType"); - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_query_type() called\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__); ret = lockdownd_send(client, dict); plist_free(dict); @@ -368,13 +374,13 @@ iphone_error_t lockdownd_query_type(lockdownd_client_t client) ret = lockdownd_recv(client, &dict); - if (IPHONE_E_SUCCESS != ret) + if (LOCKDOWN_E_SUCCESS != ret) return ret; - ret = IPHONE_E_UNKNOWN_ERROR; + ret = LOCKDOWN_E_UNKNOWN_ERROR; if (lockdown_check_result(dict, "QueryType") == RESULT_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_query_type(): success\n"); - ret = IPHONE_E_SUCCESS; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); + ret = LOCKDOWN_E_SUCCESS; } plist_free(dict); dict = NULL; @@ -389,15 +395,15 @@ iphone_error_t lockdownd_query_type(lockdownd_client_t client) * @param key the key name to request or NULL to query for all keys * @param value a plist node representing the result value node * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain, const char *key, plist_t *value) +lockdownd_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain, const char *key, plist_t *value) { if (!client) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; plist_t dict = NULL; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; /* setup request plist */ dict = plist_new_dict(); @@ -418,19 +424,19 @@ iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain plist_free(dict); dict = NULL; - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; /* Now get device's answer */ ret = lockdownd_recv(client, &dict); - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; if (lockdown_check_result(dict, "GetValue") == RESULT_SUCCESS) { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { plist_free(dict); return ret; } @@ -445,7 +451,7 @@ iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain plist_get_key_val(value_key_node, &result_key); if (!strcmp(result_key, "Value")) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_get_value(): has a value\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: has a value\n", __func__); *value = plist_copy(value_value_node); } free(result_key); @@ -462,15 +468,15 @@ iphone_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain * @param key the key name to set the value or NULL to set a value dict plist * @param value a plist node of any node type representing the value to set * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value) +lockdownd_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value) { if (!client || !value) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; plist_t dict = NULL; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; /* setup request plist */ dict = plist_new_dict(); @@ -494,20 +500,20 @@ iphone_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain plist_free(dict); dict = NULL; - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; /* Now get device's answer */ ret = lockdownd_recv(client, &dict); - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; if (lockdown_check_result(dict, "SetValue") == RESULT_SUCCESS) { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { plist_free(dict); return ret; } @@ -524,15 +530,15 @@ iphone_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain * @param domain the domain to query on or NULL for global domain * @param key the key name to remove or NULL remove all keys for the current domain * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key) +lockdownd_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key) { if (!client) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; plist_t dict = NULL; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; /* setup request plist */ dict = plist_new_dict(); @@ -553,20 +559,20 @@ iphone_error_t lockdownd_remove_value(lockdownd_client_t client, const char *dom plist_free(dict); dict = NULL; - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; /* Now get device's answer */ ret = lockdownd_recv(client, &dict); - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; if (lockdown_check_result(dict, "RemoveValue") == RESULT_SUCCESS) { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { plist_free(dict); return ret; } @@ -577,18 +583,18 @@ iphone_error_t lockdownd_remove_value(lockdownd_client_t client, const char *dom /** Asks for the device's unique id. Part of the lockdownd handshake. * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_get_device_uid(lockdownd_client_t client, char **uid) +lockdownd_error_t lockdownd_get_device_uuid(lockdownd_client_t client, char **uuid) { - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t value = NULL; ret = lockdownd_get_value(client, NULL, "UniqueDeviceID", &value); - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { return ret; } - plist_get_string_val(value, uid); + plist_get_string_val(value, uuid); plist_free(value); value = NULL; @@ -597,17 +603,17 @@ iphone_error_t lockdownd_get_device_uid(lockdownd_client_t client, char **uid) /** Askes for the device's public key. Part of the lockdownd handshake. * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key) +lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key) { - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t value = NULL; char *value_value = NULL; uint64_t size = 0; ret = lockdownd_get_value(client, NULL, "DevicePublicKey", &value); - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { return ret; } plist_get_data_val(value, &value_value, &size); @@ -625,15 +631,15 @@ iphone_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls * @param client The pointer to the location of the new lockdownd_client * * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name) +lockdownd_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name) { - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t value = NULL; ret = lockdownd_get_value(client, NULL, "DeviceName", &value); - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { return ret; } plist_get_string_val(value, device_name); @@ -649,19 +655,19 @@ iphone_error_t lockdownd_get_device_name(lockdownd_client_t client, char **devic * @param phone The iPhone to create a lockdownd client for * @param client The pointer to the location of the new lockdownd_client * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t *client) +lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_t *client) { if (!device || !client || (client && *client)) - return IPHONE_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_SUCCESS; + return LOCKDOWN_E_INVALID_ARG; + lockdownd_error_t ret = LOCKDOWN_E_SUCCESS; char *host_id = NULL; int sfd = usbmuxd_connect(device->handle, 0xf27e); if (sfd < 0) { log_debug_msg("%s: could not connect to lockdownd (device handle %d)\n", __func__, device->handle); - return IPHONE_E_UNKNOWN_ERROR; + return LOCKDOWN_E_MUX_ERROR; } lockdownd_client_t client_loc = (lockdownd_client_t) malloc(sizeof(struct lockdownd_client_int)); @@ -669,36 +675,36 @@ iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t * client_loc->ssl_session = (gnutls_session_t *) malloc(sizeof(gnutls_session_t)); client_loc->in_SSL = 0; - if (IPHONE_E_SUCCESS != lockdownd_query_type(client_loc)) { - log_debug_msg("QueryType failed in the lockdownd client.\n"); - ret = IPHONE_E_NOT_ENOUGH_DATA; + if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc)) { + log_debug_msg("%s: QueryType failed in the lockdownd client.\n", __func__); + ret = LOCKDOWN_E_NOT_ENOUGH_DATA; } - char *uid = NULL; - ret = lockdownd_get_device_uid(client_loc, &uid); - if (IPHONE_E_SUCCESS != ret) { - log_debug_msg("Device refused to send uid.\n"); + char *uuid = NULL; + ret = iphone_device_get_uuid(device, &uuid); + if (LOCKDOWN_E_SUCCESS != ret) { + log_debug_msg("%s: failed to get device uuid.\n", __func__); } - log_debug_msg("Device uid: %s\n", uid); + log_debug_msg("%s: device uuid: %s\n", __func__, uuid); - host_id = get_host_id(); - if (IPHONE_E_SUCCESS == ret && !host_id) { - ret = IPHONE_E_INVALID_CONF; + userpref_get_host_id(&host_id); + if (LOCKDOWN_E_SUCCESS == ret && !host_id) { + ret = LOCKDOWN_E_INVALID_CONF; } - if (IPHONE_E_SUCCESS == ret && !is_device_known(uid)) - ret = lockdownd_pair(client_loc, uid, host_id); + if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(uuid)) + ret = lockdownd_pair(client_loc, uuid, host_id); - if (uid) { - free(uid); - uid = NULL; + if (uuid) { + free(uuid); + uuid = NULL; } - if (IPHONE_E_SUCCESS == ret) { + if (LOCKDOWN_E_SUCCESS == ret) { ret = lockdownd_start_ssl_session(client_loc, host_id); - if (IPHONE_E_SUCCESS != ret) { - ret = IPHONE_E_SSL_ERROR; - log_debug_msg("SSL Session opening failed.\n"); + if (LOCKDOWN_E_SUCCESS != ret) { + ret = LOCKDOWN_E_SSL_ERROR; + log_debug_msg("%s: SSL Session opening failed.\n", __func__); } if (host_id) { @@ -706,7 +712,7 @@ iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t * host_id = NULL; } - if (IPHONE_E_SUCCESS == ret) + if (LOCKDOWN_E_SUCCESS == ret) *client = client_loc; } @@ -716,11 +722,11 @@ iphone_error_t lockdownd_new_client(iphone_device_t device, lockdownd_client_t * /** Generates the appropriate keys and pairs the device. It's part of the * lockdownd handshake. * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_id) +lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *host_id) { - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = NULL; plist_t dict_record = NULL; @@ -730,14 +736,14 @@ iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_i gnutls_datum_t public_key = { NULL, 0 }; ret = lockdownd_get_device_public_key(client, &public_key); - if (ret != IPHONE_E_SUCCESS) { - log_debug_msg("Device refused to send public key.\n"); + if (ret != LOCKDOWN_E_SUCCESS) { + log_debug_msg("%s: device refused to send public key.\n", __func__); return ret; } - log_debug_msg("device public key :\n %s.\n", public_key.data); + log_debug_msg("%s: device public key follows:\n%s\n", __func__, public_key.data); ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert); - if (ret != IPHONE_E_SUCCESS) { + if (ret != LOCKDOWN_E_SUCCESS) { free(public_key.data); return ret; } @@ -763,29 +769,28 @@ iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_i plist_free(dict); dict = NULL; - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; /* Now get iPhone's answer */ ret = lockdownd_recv(client, &dict); - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; if (lockdown_check_result(dict, "Pair") == RESULT_SUCCESS) { - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } plist_free(dict); dict = NULL; /* store public key in config if pairing succeeded */ - if (ret == IPHONE_E_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair: pair success\n"); - store_device_public_key(uid, public_key); - ret = IPHONE_E_SUCCESS; + if (ret == LOCKDOWN_E_SUCCESS) { + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair success\n", __func__); + userpref_set_device_public_key(uuid, public_key); } else { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair: pair failure\n"); - ret = IPHONE_E_PAIRING_FAILED; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair failure\n", __func__); + ret = LOCKDOWN_E_PAIRING_FAILED; } free(public_key.data); return ret; @@ -796,20 +801,20 @@ iphone_error_t lockdownd_pair(lockdownd_client_t client, char *uid, char *host_i * * @param client The lockdown client * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_enter_recovery(lockdownd_client_t client) +lockdownd_error_t lockdownd_enter_recovery(lockdownd_client_t client) { if (!client) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); plist_add_sub_key_el(dict, "Request"); plist_add_sub_string_el(dict, "EnterRecovery"); - log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Telling device to enter recovery mode\n", __func__); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: telling device to enter recovery mode\n", __func__); ret = lockdownd_send(client, dict); plist_free(dict); @@ -819,7 +824,7 @@ iphone_error_t lockdownd_enter_recovery(lockdownd_client_t client) if (lockdown_check_result(dict, "EnterRecovery") == RESULT_SUCCESS) { log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } plist_free(dict); dict = NULL; @@ -832,35 +837,34 @@ iphone_error_t lockdownd_enter_recovery(lockdownd_client_t client) * * @param client The lockdown client * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_goodbye(lockdownd_client_t client) +lockdownd_error_t lockdownd_goodbye(lockdownd_client_t client) { if (!client) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); plist_add_sub_key_el(dict, "Request"); plist_add_sub_string_el(dict, "Goodbye"); - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_goodbye() called\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__); ret = lockdownd_send(client, dict); plist_free(dict); dict = NULL; ret = lockdownd_recv(client, &dict); - if (!dict) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_goodbye(): IPHONE_E_PLIST_ERROR\n"); - return IPHONE_E_PLIST_ERROR; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: did not get goodbye response back\n", __func__); + return LOCKDOWN_E_PLIST_ERROR; } if (lockdown_check_result(dict, "Goodbye") == RESULT_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_goodbye(): success\n"); - ret = IPHONE_E_SUCCESS; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: success\n", __func__); + ret = LOCKDOWN_E_SUCCESS; } plist_free(dict); dict = NULL; @@ -870,14 +874,15 @@ iphone_error_t lockdownd_goodbye(lockdownd_client_t client) /** Generates the device certificate from the public key as well as the host * and root certificates. * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert, +lockdownd_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert, gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert) { if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert) - return IPHONE_E_INVALID_ARG; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + return LOCKDOWN_E_INVALID_ARG; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; + userpref_error_t uret = USERPREF_E_UNKNOWN_ERROR; gnutls_datum_t modulus = { NULL, 0 }; gnutls_datum_t exponent = { NULL, 0 }; @@ -905,7 +910,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, (int*)&modulus.size); ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, (int*)&exponent.size); if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2) - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } if (asn1_pub_key) asn1_delete_structure(&asn1_pub_key); @@ -915,7 +920,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t } /* now generate certifcates */ - if (IPHONE_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) { + if (LOCKDOWN_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) { gnutls_global_init(); gnutls_datum_t essentially_null = { (unsigned char*)strdup("abababababababab"), strlen("abababababababab") }; @@ -935,10 +940,9 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t gnutls_x509_privkey_init(&root_privkey); gnutls_x509_privkey_init(&host_privkey); - ret = get_keys_and_certs(root_privkey, root_cert, host_privkey, host_cert); - - if (IPHONE_E_SUCCESS == ret) { + uret = userpref_get_keys_and_certs(root_privkey, root_cert, host_privkey, host_cert); + if (USERPREF_E_SUCCESS == uret) { /* generate device certificate */ gnutls_x509_crt_set_key(dev_cert, fake_privkey); gnutls_x509_crt_set_serial(dev_cert, "\x00", 1); @@ -948,7 +952,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey); - if (IPHONE_E_SUCCESS == ret) { + if (LOCKDOWN_E_SUCCESS == ret) { /* if everything went well, export in PEM format */ gnutls_datum_t dev_pem = { NULL, 0 }; gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); @@ -958,7 +962,9 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t gnutls_datum_t pem_root_cert = { NULL, 0 }; gnutls_datum_t pem_host_cert = { NULL, 0 }; - if ( IPHONE_E_SUCCESS == get_certs_as_pem(&pem_root_cert, &pem_host_cert) ) { + uret = userpref_get_certs_as_pem(&pem_root_cert, &pem_host_cert); + + if (USERPREF_E_SUCCESS == uret) { /* copy buffer for output */ odevice_cert->data = malloc(dev_pem.size); memcpy(odevice_cert->data, dev_pem.data, dev_pem.size); @@ -977,6 +983,19 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t } } } + + switch(uret) { + case USERPREF_E_INVALID_ARG: + ret = LOCKDOWN_E_INVALID_ARG; + break; + case USERPREF_E_INVALID_CONF: + ret = LOCKDOWN_E_INVALID_CONF; + break; + case USERPREF_E_SSL_ERROR: + ret = LOCKDOWN_E_SSL_ERROR; + default: + break; + } } } @@ -993,14 +1012,14 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * @param client The lockdownd client * @param HostID The HostID used with this phone * - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char *HostID) +lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char *HostID) { plist_t dict = NULL; uint32_t return_me = 0; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; client->session_id[0] = '\0'; /* Setup DevicePublicKey request plist */ @@ -1014,13 +1033,13 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char plist_free(dict); dict = NULL; - if (ret != IPHONE_E_SUCCESS) + if (ret != LOCKDOWN_E_SUCCESS) return ret; ret = lockdownd_recv(client, &dict); if (!dict) - return IPHONE_E_PLIST_ERROR; + return LOCKDOWN_E_PLIST_ERROR; if (lockdown_check_result(dict, "StartSession") == RESULT_FAILURE) { plist_t error_node = plist_get_dict_el_from_key(dict, "Error"); @@ -1029,12 +1048,14 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char plist_get_string_val(error_node, &error); if (!strcmp(error, "InvalidHostID")) { - //hostid is unknown. Pair and try again - char *uid = NULL; - char* host_id = get_host_id(); - if (IPHONE_E_SUCCESS == lockdownd_get_device_uid(client, &uid) ) { - if (IPHONE_E_SUCCESS == lockdownd_pair(client, uid, host_id) ) { - //start session again + /* hostid is unknown. Pair and try again */ + char *uuid = NULL; + char *host_id = NULL; + userpref_get_host_id(&host_id); + + if (LOCKDOWN_E_SUCCESS == lockdownd_get_device_uuid(client, &uuid) ) { + if (LOCKDOWN_E_SUCCESS == lockdownd_pair(client, uuid, host_id) ) { + /* start session again */ plist_free(dict); dict = plist_new_dict(); plist_add_sub_key_el(dict, "HostID"); @@ -1049,20 +1070,20 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char ret = lockdownd_recv(client, &dict); } } - free(uid); + free(uuid); free(host_id); } free(error); } } - ret = IPHONE_E_SSL_ERROR; + ret = LOCKDOWN_E_SSL_ERROR; if (lockdown_check_result(dict, "StartSession") == RESULT_SUCCESS) { // Set up GnuTLS... //gnutls_anon_client_credentials_t anoncred; gnutls_certificate_credentials_t xcred; - log_dbg_msg(DBGMASK_LOCKDOWND, "We started the session OK, now trying GnuTLS\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: started the session OK, now trying GnuTLS\n", __func__); errno = 0; gnutls_global_init(); //gnutls_anon_allocate_client_credentials(&anoncred); @@ -1084,30 +1105,29 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char } gnutls_credentials_set(*client->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. - log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 1...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 1...\n", __func__); gnutls_transport_set_ptr(*client->ssl_session, (gnutls_transport_ptr_t) client); - log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 2...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 2...\n", __func__); gnutls_transport_set_push_function(*client->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); - log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 3...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 3...\n", __func__); gnutls_transport_set_pull_function(*client->ssl_session, (gnutls_pull_func) & lockdownd_securead); - log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 4 -- now handshaking...\n"); - + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS step 4 -- now handshaking...\n", __func__); if (errno) - log_dbg_msg(DBGMASK_LOCKDOWND, "WARN: errno says %s before handshake!\n", strerror(errno)); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: WARN: errno says %s before handshake!\n", __func__, strerror(errno)); return_me = gnutls_handshake(*client->ssl_session); - log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS handshake done...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS handshake done...\n", __func__); if (return_me != GNUTLS_E_SUCCESS) { - log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS reported something wrong.\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: GnuTLS reported something wrong.\n", __func__); gnutls_perror(return_me); - log_dbg_msg(DBGMASK_LOCKDOWND, "oh.. errno says %s\n", strerror(errno)); - return IPHONE_E_SSL_ERROR; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: oh.. errno says %s\n", __func__, strerror(errno)); + return LOCKDOWN_E_SSL_ERROR; } else { client->in_SSL = 1; - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } } - //store session id + /* store session id */ plist_t session_node = plist_find_node_by_key(dict, "SessionID"); if (session_node) { @@ -1120,23 +1140,23 @@ iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char plist_get_string_val(session_node_val, &session_id); if (session_node_val_type == PLIST_STRING && session_id) { - // we need to store the session ID for StopSession + /* we need to store the session ID for StopSession */ strcpy(client->session_id, session_id); - log_dbg_msg(DBGMASK_LOCKDOWND, "SessionID: %s\n", client->session_id); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: SessionID: %s\n", __func__, client->session_id); } if (session_id) free(session_id); } } else - log_dbg_msg(DBGMASK_LOCKDOWND, "Failed to get SessionID!\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Failed to get SessionID!\n", __func__); plist_free(dict); dict = NULL; - if (ret == IPHONE_E_SUCCESS) + if (ret == LOCKDOWN_E_SUCCESS) return ret; - log_dbg_msg(DBGMASK_LOCKDOWND, "Apparently failed negotiating with lockdownd.\n"); - return IPHONE_E_SSL_ERROR; + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Apparently failed negotiating with lockdownd.\n", __func__); + return LOCKDOWN_E_SSL_ERROR; } /** gnutls callback for writing data to the iPhone. @@ -1152,12 +1172,10 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size uint32_t bytes = 0; lockdownd_client_t client; client = (lockdownd_client_t) transport; - log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_secuwrite() called\n"); - log_dbg_msg(DBGMASK_LOCKDOWND, "pre-send\nlength = %zi\n", length); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pre-send length = %zi\n", __func__, length); usbmuxd_send(client->sfd, buffer, length, &bytes); - log_dbg_msg(DBGMASK_LOCKDOWND, "post-send\nsent %i bytes\n", bytes); - - dump_debug_buffer("sslpacketwrite.out", buffer, length); + log_dbg_msg(DBGMASK_LOCKDOWND, "%s: post-send sent %i bytes\n", __func__, bytes); return bytes; } @@ -1179,19 +1197,17 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ client = (lockdownd_client_t) transport; char *recv_buffer; - log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length); - - log_debug_msg("pre-read\nclient wants %zi bytes\n", length); + log_debug_msg("%s: pre-read client wants %zi bytes\n", __func__, length); recv_buffer = (char *) malloc(sizeof(char) * this_len); // repeat until we have the full data or an error occurs. do { - if ((res = usbmuxd_recv(client->sfd, recv_buffer, this_len, (uint32_t*)&bytes)) != IPHONE_E_SUCCESS) { + if ((res = usbmuxd_recv(client->sfd, recv_buffer, this_len, (uint32_t*)&bytes)) != LOCKDOWN_E_SUCCESS) { log_debug_msg("%s: ERROR: usbmux_recv returned %d\n", __func__, res); return res; } - log_debug_msg("post-read\nwe got %i bytes\n", bytes); + log_debug_msg("%s: post-read we got %i bytes\n", __func__, bytes); // increase read count tbytes += bytes; @@ -1205,7 +1221,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ } this_len = length - tbytes; - log_debug_msg("re-read\ntrying to read missing %i bytes\n", this_len); + log_debug_msg("%s: re-read trying to read missing %i bytes\n", __func__, this_len); } while (tbytes < length); if (recv_buffer) { @@ -1221,22 +1237,23 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ * @param service The name of the service to start * @param port The port number the service was started on - * @return an error code (IPHONE_E_SUCCESS on success) + * @return an error code (LOCKDOWN_E_SUCCESS on success) */ -iphone_error_t lockdownd_start_service(lockdownd_client_t client, const char *service, int *port) +lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, const char *service, int *port) { if (!client || !service || !port) - return IPHONE_E_INVALID_ARG; + return LOCKDOWN_E_INVALID_ARG; - char *host_id = get_host_id(); + char *host_id = NULL; + userpref_get_host_id(&host_id); if (!host_id) - return IPHONE_E_INVALID_CONF; + return LOCKDOWN_E_INVALID_CONF; if (!client->in_SSL && !lockdownd_start_ssl_session(client, host_id)) - return IPHONE_E_SSL_ERROR; + return LOCKDOWN_E_SSL_ERROR; plist_t dict = NULL; uint32_t port_loc = 0; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; free(host_id); host_id = NULL; @@ -1252,18 +1269,18 @@ iphone_error_t lockdownd_start_service(lockdownd_client_t client, const char *se plist_free(dict); dict = NULL; - if (IPHONE_E_SUCCESS != ret) + if (LOCKDOWN_E_SUCCESS != ret) return ret; ret = lockdownd_recv(client, &dict); - if (IPHONE_E_SUCCESS != ret) + if (LOCKDOWN_E_SUCCESS != ret) return ret; if (!dict) - return IPHONE_E_PLIST_ERROR; + return LOCKDOWN_E_PLIST_ERROR; - ret = IPHONE_E_UNKNOWN_ERROR; + ret = LOCKDOWN_E_UNKNOWN_ERROR; if (lockdown_check_result(dict, "StartService") == RESULT_SUCCESS) { plist_t port_key_node = plist_find_node_by_key(dict, "Port"); plist_t port_value_node = plist_get_next_sibling(port_key_node); @@ -1277,17 +1294,17 @@ iphone_error_t lockdownd_start_service(lockdownd_client_t client, const char *se plist_get_uint_val(port_value_node, &port_value); if (port_key && !strcmp(port_key, "Port")) { port_loc = port_value; - ret = IPHONE_E_SUCCESS; + ret = LOCKDOWN_E_SUCCESS; } if (port_key) free(port_key); - if (port && ret == IPHONE_E_SUCCESS) + if (port && ret == LOCKDOWN_E_SUCCESS) *port = port_loc; } } else - ret = IPHONE_E_START_SERVICE_FAILED; + ret = LOCKDOWN_E_START_SERVICE_FAILED; plist_free(dict); dict = NULL; diff --git a/src/lockdown.h b/src/lockdown.h index 185d27a..1e193f6 100644 --- a/src/lockdown.h +++ b/src/lockdown.h @@ -24,6 +24,7 @@ #include <gnutls/gnutls.h> #include <string.h> + #include "libiphone/lockdown.h" struct lockdownd_client_int { @@ -33,13 +34,14 @@ struct lockdownd_client_int { char session_id[40]; }; -iphone_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); -iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * device_cert, +lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); +lockdownd_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * device_cert, gnutls_datum_t * host_cert, gnutls_datum_t * root_cert); -// SSL functions -iphone_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char *HostID); +/* SSL functions */ +lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const char *HostID); ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length); ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length); + #endif diff --git a/src/userpref.c b/src/userpref.c index 0e83133..4b6dd98 100644 --- a/src/userpref.c +++ b/src/userpref.c @@ -22,6 +22,7 @@ #include <glib.h> #include <glib/gprintf.h> #include <stdio.h> +#include <stdint.h> #include <stdlib.h> #include <string.h> #include <gnutls/gnutls.h> @@ -42,7 +43,7 @@ /** Creates a freedesktop compatible configuration directory for libiphone. */ -static void create_config_dir(void) +static void userpref_create_config_dir(void) { gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL); @@ -62,9 +63,10 @@ static int get_rand(int min, int max) * * @return A null terminated string containing a valid HostID. */ -static char *lockdownd_generate_hostid() +static char *userpref_generate_host_id() { - char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long + /* HostID's are just UUID's, and UUID's are 36 characters long */ + char *hostid = (char *) malloc(sizeof(char) * 37); const char *chars = "ABCDEF0123456789"; srand(time(NULL)); int i = 0; @@ -77,7 +79,8 @@ static char *lockdownd_generate_hostid() hostid[i] = chars[get_rand(0, 16)]; } } - hostid[36] = '\0'; // make it a real string + /* make it a real string */ + hostid[36] = '\0'; return hostid; } @@ -85,7 +88,7 @@ static char *lockdownd_generate_hostid() * * @param host_id A null terminated string containing a valid HostID. */ -static int write_host_id(char *host_id) +static int userpref_set_host_id(char *host_id) { GKeyFile *key_file; gsize length; @@ -96,13 +99,13 @@ static int write_host_id(char *host_id) return 0; /* Make sure config directory exists */ - create_config_dir(); + userpref_create_config_dir(); /* Now parse file to get the HostID */ key_file = g_key_file_new(); /* Store in config file */ - log_debug_msg("init_config_file(): setting hostID to %s\n", host_id); + log_debug_msg("%s: setting hostID to %s\n", __func__, host_id); g_key_file_set_value(key_file, "Global", "HostID", host_id); /* Write config file on disk */ @@ -125,9 +128,8 @@ static int write_host_id(char *host_id) * * @return The string containing the HostID or NULL */ -char *get_host_id(void) +void userpref_get_host_id(char **host_id) { - char *host_id = NULL; gchar *config_file; GKeyFile *key_file; gchar *loc_host_id; @@ -140,20 +142,19 @@ char *get_host_id(void) 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 (!host_id) { - //no config, generate host_id - host_id = lockdownd_generate_hostid(); - write_host_id(host_id); + /* no config, generate host_id */ + *host_id = userpref_generate_host_id(); + userpref_set_host_id(*host_id); } - log_debug_msg("get_host_id(): Using %s as HostID\n", host_id); - return host_id; + log_debug_msg("%s: Using %s as HostID\n", __func__, *host_id); } /** Determines whether this iPhone has been connected to this system before. @@ -163,13 +164,13 @@ char *get_host_id(void) * @return 1 if the iPhone has been connected previously to this configuration * or 0 otherwise. */ -int is_device_known(char *uid) +int userpref_has_device_public_key(char *uuid) { int ret = 0; gchar *config_file; /* first get config file */ - gchar *device_file = g_strconcat(uid, ".pem", NULL); + gchar *device_file = g_strconcat(uuid, ".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; @@ -186,17 +187,19 @@ 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, gnutls_datum_t public_key) +userpref_error_t userpref_set_device_public_key(char *uuid, gnutls_datum_t public_key) { - - if (NULL == public_key.data || is_device_known(uid)) - return 0; + if (NULL == public_key.data) + return USERPREF_E_INVALID_ARG; + + if (userpref_has_device_public_key(uuid)) + return USERPREF_E_SUCCESS; /* ensure config directory exists */ - create_config_dir(); + userpref_create_config_dir(); /* build file path */ - gchar *device_file = g_strconcat(uid, ".pem", NULL); + gchar *device_file = g_strconcat(uuid, ".pem", NULL); gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); /* store file */ @@ -205,7 +208,8 @@ int store_device_public_key(char *uid, gnutls_datum_t public_key) fclose(pFile); g_free(pem); g_free(device_file); - return 1; + + return USERPREF_E_SUCCESS; } /** Private function which reads the given file into a gnutls structure. @@ -215,7 +219,7 @@ int store_device_public_key(char *uid, gnutls_datum_t public_key) * * @return 1 if the file contents where read successfully and 0 otherwise. */ -static int read_file_in_confdir(const char *file, gnutls_datum_t * data) +static int userpref_get_file_contents(const char *file, gnutls_datum_t * data) { gboolean success; gsize size; @@ -237,17 +241,17 @@ static int read_file_in_confdir(const char *file, gnutls_datum_t * data) return success; } - /** Private function which generate private keys and certificates. * - * @return IPHONE_E_SUCCESS if keys were successfully generated. + * @return 1 if keys were successfully generated, 0 otherwise */ -static iphone_error_t gen_keys_and_cert(void) +static userpref_error_t userpref_gen_keys_and_cert(void) { - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + userpref_error_t ret = USERPREF_E_SSL_ERROR; + gnutls_x509_privkey_t root_privkey; - gnutls_x509_privkey_t host_privkey; gnutls_x509_crt_t root_cert; + gnutls_x509_privkey_t host_privkey; gnutls_x509_crt_t host_cert; gnutls_global_deinit(); @@ -275,7 +279,6 @@ static iphone_error_t gen_keys_and_cert(void) gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); gnutls_x509_crt_sign(root_cert, root_cert, root_privkey); - gnutls_x509_crt_set_key(host_cert, host_privkey); gnutls_x509_crt_set_serial(host_cert, "\x00", 1); gnutls_x509_crt_set_version(host_cert, 3); @@ -312,14 +315,14 @@ static iphone_error_t gen_keys_and_cert(void) if (NULL != root_cert_pem.data && 0 != root_cert_pem.size && NULL != host_cert_pem.data && 0 != host_cert_pem.size) - ret = IPHONE_E_SUCCESS; + ret = USERPREF_E_SUCCESS; /* store values in config file */ - init_config_file( &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem); + userpref_set_keys_and_certs( &root_key_pem, &root_cert_pem, &host_key_pem, &host_cert_pem); gnutls_free(root_key_pem.data); - gnutls_free(host_key_pem.data); gnutls_free(root_cert_pem.data); + gnutls_free(host_key_pem.data); gnutls_free(host_cert_pem.data); //restore gnutls env @@ -334,18 +337,18 @@ static iphone_error_t gen_keys_and_cert(void) * @param key_name The filename of the private key to import. * @param key the gnutls key structure. * - * @return IPHONE_E_SUCCESS if the key was successfully imported. + * @return 1 if the key was successfully imported. */ -static iphone_error_t import_key(const char* key_name, gnutls_x509_privkey_t key) +static userpref_error_t userpref_import_key(const char* key_name, gnutls_x509_privkey_t key) { - iphone_error_t ret = IPHONE_E_INVALID_CONF; + userpref_error_t ret = USERPREF_E_INVALID_CONF; gnutls_datum_t pem_key = { NULL, 0 }; - if ( read_file_in_confdir(key_name, &pem_key) ) { + if (userpref_get_file_contents(key_name, &pem_key)) { if (GNUTLS_E_SUCCESS == gnutls_x509_privkey_import(key, &pem_key, GNUTLS_X509_FMT_PEM)) - ret = IPHONE_E_SUCCESS; + ret = USERPREF_E_SUCCESS; else - ret = IPHONE_E_SSL_ERROR; + ret = USERPREF_E_SSL_ERROR; } gnutls_free(pem_key.data); return ret; @@ -358,16 +361,16 @@ static iphone_error_t import_key(const char* key_name, gnutls_x509_privkey_t key * * @return IPHONE_E_SUCCESS if the certificate was successfully imported. */ -static iphone_error_t import_crt(const char* crt_name, gnutls_x509_crt_t cert) +static userpref_error_t userpref_import_crt(const char* crt_name, gnutls_x509_crt_t cert) { - iphone_error_t ret = IPHONE_E_INVALID_CONF; + userpref_error_t ret = USERPREF_E_INVALID_CONF; gnutls_datum_t pem_cert = { NULL, 0 }; - if ( read_file_in_confdir(crt_name, &pem_cert) ) { + if (userpref_get_file_contents(crt_name, &pem_cert)) { if (GNUTLS_E_SUCCESS == gnutls_x509_crt_import(cert, &pem_cert, GNUTLS_X509_FMT_PEM)) - ret = IPHONE_E_SUCCESS; + ret = USERPREF_E_SUCCESS; else - ret = IPHONE_E_SSL_ERROR; + ret = USERPREF_E_SSL_ERROR; } gnutls_free(pem_cert.data); return ret; @@ -383,41 +386,41 @@ static iphone_error_t import_crt(const char* crt_name, gnutls_x509_crt_t cert) * @param host_privkey The host private key. * @param host_crt The host certificate. * - * @return IPHONE_E_SUCCESS if the keys and certificates were successfully retrieved. + * @return 1 if the keys and certificates were successfully retrieved, 0 otherwise */ -iphone_error_t get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt) +userpref_error_t userpref_get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt) { - iphone_error_t ret = IPHONE_E_SUCCESS; + userpref_error_t ret = USERPREF_E_SUCCESS; - if (ret == IPHONE_E_SUCCESS) - ret = import_key(LIBIPHONE_ROOT_PRIVKEY, root_privkey); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_key(LIBIPHONE_ROOT_PRIVKEY, root_privkey); - if (ret == IPHONE_E_SUCCESS) - ret = import_key(LIBIPHONE_HOST_PRIVKEY, host_privkey); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_key(LIBIPHONE_HOST_PRIVKEY, host_privkey); - if (ret == IPHONE_E_SUCCESS) - ret = import_crt(LIBIPHONE_ROOT_CERTIF, root_crt); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_crt(LIBIPHONE_ROOT_CERTIF, root_crt); - if (ret == IPHONE_E_SUCCESS) - ret = import_crt(LIBIPHONE_HOST_CERTIF, host_crt); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_crt(LIBIPHONE_HOST_CERTIF, host_crt); - if (IPHONE_E_SUCCESS != ret) { + if (USERPREF_E_SUCCESS != ret) { //we had problem reading or importing root cert //try with a new ones. - ret = gen_keys_and_cert(); + ret = userpref_gen_keys_and_cert(); - if (ret == IPHONE_E_SUCCESS) - ret = import_key(LIBIPHONE_ROOT_PRIVKEY, root_privkey); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_key(LIBIPHONE_ROOT_PRIVKEY, root_privkey); - if (ret == IPHONE_E_SUCCESS) - ret = import_key(LIBIPHONE_HOST_PRIVKEY, host_privkey); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_key(LIBIPHONE_HOST_PRIVKEY, host_privkey); - if (ret == IPHONE_E_SUCCESS) - ret = import_crt(LIBIPHONE_ROOT_CERTIF, root_crt); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_crt(LIBIPHONE_ROOT_CERTIF, root_crt); - if (ret == IPHONE_E_SUCCESS) - ret = import_crt(LIBIPHONE_HOST_CERTIF, host_crt); + if (ret == USERPREF_E_SUCCESS) + ret = userpref_import_crt(LIBIPHONE_HOST_CERTIF, host_crt); } return ret; @@ -428,46 +431,43 @@ iphone_error_t get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x50 * @param pem_root_cert The root certificate. * @param pem_host_cert The host certificate. * - * @return IPHONE_E_SUCCESS if the certificates were successfully retrieved. + * @return 1 if the certificates were successfully retrieved, 0 otherwise */ -iphone_error_t get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert) +userpref_error_t userpref_get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert) { - iphone_error_t ret = IPHONE_E_INVALID_CONF; - - if ( !pem_root_cert || !pem_host_cert) - return IPHONE_E_INVALID_ARG; + if (!pem_root_cert || !pem_host_cert) + return USERPREF_E_INVALID_ARG; - if ( read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, pem_root_cert) && read_file_in_confdir(LIBIPHONE_HOST_CERTIF, pem_host_cert)) - ret = IPHONE_E_SUCCESS; + if (userpref_get_file_contents(LIBIPHONE_ROOT_CERTIF, pem_root_cert) && userpref_get_file_contents(LIBIPHONE_HOST_CERTIF, pem_host_cert)) + return USERPREF_E_SUCCESS; else { g_free(pem_root_cert->data); g_free(pem_host_cert->data); } - return ret; + return USERPREF_E_INVALID_CONF; } + /** Create and save a configuration file containing the given data. * * @note: All fields must specified and be non-null * - * @param host_id The UUID of the host * @param root_key The root key - * @param host_key The host key * @param root_cert The root certificate + * @param host_key The host key * @param host_cert The host certificate * * @return 1 on success and 0 otherwise. */ -int init_config_file( gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, - gnutls_datum_t * host_cert) +userpref_error_t userpref_set_keys_and_certs(gnutls_datum_t * root_key, gnutls_datum_t * root_cert, gnutls_datum_t * host_key, gnutls_datum_t * host_cert) { FILE *pFile; gchar *pem; if (!root_key || !host_key || !root_cert || !host_cert) - return 0; + return USERPREF_E_INVALID_ARG; /* Make sure config directory exists */ - create_config_dir(); + userpref_create_config_dir(); /* 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); @@ -494,5 +494,5 @@ int init_config_file( gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnut fclose(pFile); g_free(pem); - return 1; + return USERPREF_E_SUCCESS; } diff --git a/src/userpref.h b/src/userpref.h index deced04..414c093 100644 --- a/src/userpref.h +++ b/src/userpref.h @@ -23,19 +23,21 @@ #define USERPREF_H #include <gnutls/gnutls.h> -#include "libiphone/libiphone.h" +#define USERPREF_E_SUCCESS 0 +#define USERPREF_E_INVALID_ARG -1 +#define USERPREF_E_INVALID_CONF -2 +#define USERPREF_E_SSL_ERROR -3 -iphone_error_t get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt); +#define USERPREF_E_UNKNOWN_ERROR -256 -iphone_error_t get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert); +typedef int16_t userpref_error_t; -char *get_host_id(void); +userpref_error_t userpref_get_keys_and_certs(gnutls_x509_privkey_t root_privkey, gnutls_x509_crt_t root_crt, gnutls_x509_privkey_t host_privkey, gnutls_x509_crt_t host_crt); +userpref_error_t userpref_set_keys_and_certs(gnutls_datum_t * root_key, gnutls_datum_t * root_cert, gnutls_datum_t * host_key, gnutls_datum_t * host_cert); +userpref_error_t userpref_get_certs_as_pem(gnutls_datum_t *pem_root_cert, gnutls_datum_t *pem_host_cert); +userpref_error_t userpref_set_device_public_key(char *uuid, gnutls_datum_t public_key); +int userpref_has_device_public_key(char *uuid); +void userpref_get_host_id(char **host_id); -int is_device_known(char *uid); - -int store_device_public_key(char *uid, gnutls_datum_t public_key); - -int init_config_file( gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert, - gnutls_datum_t * host_cert); #endif diff --git a/src/utils.c b/src/utils.c index 5b0872d..3c08351 100644 --- a/src/utils.c +++ b/src/utils.c @@ -20,7 +20,10 @@ */ #include <stdarg.h> #include <stdio.h> +#include <stdint.h> + #include "utils.h" +#include "libiphone/libiphone.h" int toto_debug = 0; uint16_t dbg_mask = 0; @@ -31,7 +34,7 @@ uint16_t dbg_mask = 0; * * @param level Set to 0 for no debugging or 1 for debugging. */ -void iphone_set_debug(int level) +void iphone_set_debug_level(int level) { toto_debug = level; } @@ -116,13 +119,12 @@ inline void log_debug_buffer(const char *data, const int length) inline void dump_debug_buffer(const char *file, const char *data, const int length) { #ifndef STRIP_DEBUG_CODE - /* run the real fprintf */ if (toto_debug) { FILE *my_ssl_packet = fopen(file, "w+"); fwrite(data, 1, length, my_ssl_packet); fflush(my_ssl_packet); - fprintf(stderr, "Wrote SSL packet to drive, too.\n"); + fprintf(stderr, "%s: Wrote SSL packet to drive, too.\n", __func__); fclose(my_ssl_packet); } #endif diff --git a/src/utils.h b/src/utils.h index 1750b8e..430e812 100644 --- a/src/utils.h +++ b/src/utils.h @@ -22,13 +22,10 @@ #ifndef UTILS_H #define UTILS_H -#include "libiphone/libiphone.h" - - - inline void log_debug_msg(const char *format, ...); inline void log_dbg_msg(uint16_t id, const char *format, ...); inline void log_debug_buffer(const char *data, const int length); inline void dump_debug_buffer(const char *file, const char *data, const int length); + #endif |