diff options
| author | 2009-12-10 18:40:08 +0100 | |
|---|---|---|
| committer | 2009-12-10 18:40:08 +0100 | |
| commit | 7b153ef8ca43c4b437cb9e4e6f2777bdb7047677 (patch) | |
| tree | 04472b547ed5363dc1d6d4b9c4766823683ebc9d /src | |
| parent | 444b4d94afa6ff15129922f0dc2b82ae69deeac8 (diff) | |
| parent | 318cc4f7b336109819c7b4c6a1a9f2e8d37d9bed (diff) | |
| download | libimobiledevice-7b153ef8ca43c4b437cb9e4e6f2777bdb7047677.tar.gz libimobiledevice-7b153ef8ca43c4b437cb9e4e6f2777bdb7047677.tar.bz2 | |
Merge branch 'master' of git://github.com/MattColyer/libiphone into martin
Diffstat (limited to 'src')
| -rw-r--r-- | src/AFC.c | 266 | ||||
| -rw-r--r-- | src/AFC.h | 12 | ||||
| -rw-r--r-- | src/lockdown.c | 164 | ||||
| -rw-r--r-- | src/lockdown.h | 3 |
4 files changed, 261 insertions, 184 deletions
| @@ -119,21 +119,24 @@ afc_error_t afc_client_free(afc_client_t client) | |||
| 119 | * @param client The client to send data through. | 119 | * @param client The client to send data through. |
| 120 | * @param data The data to send. | 120 | * @param data The data to send. |
| 121 | * @param length The length to send. | 121 | * @param length The length to send. |
| 122 | * | 122 | * @param bytes_sent The number of bytes actually sent. |
| 123 | * @return The number of bytes actually sent, or -1 on error. | 123 | * |
| 124 | * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error. | ||
| 124 | * | 125 | * |
| 125 | * @warning set client->afc_packet->this_length and | 126 | * @warning set client->afc_packet->this_length and |
| 126 | * client->afc_packet->entire_length to 0 before calling this. The | 127 | * client->afc_packet->entire_length to 0 before calling this. The |
| 127 | * reason is that if you set them to different values, it indicates | 128 | * reason is that if you set them to different values, it indicates |
| 128 | * you want to send the data as two packets. | 129 | * you want to send the data as two packets. |
| 129 | */ | 130 | */ |
| 130 | static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t length) | 131 | static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, uint32_t length, uint32_t *bytes_sent) |
| 131 | { | 132 | { |
| 132 | int bytes = 0, offset = 0; | 133 | uint32_t offset = 0; |
| 133 | uint32_t sent = 0; | 134 | uint32_t sent = 0; |
| 134 | 135 | ||
| 135 | if (!client || !client->connection || !client->afc_packet) | 136 | if (!client || !client->connection || !client->afc_packet) |
| 136 | return 0; | 137 | return AFC_E_INVALID_ARGUMENT; |
| 138 | |||
| 139 | *bytes_sent = 0; | ||
| 137 | 140 | ||
| 138 | if (!data || !length) | 141 | if (!data || !length) |
| 139 | length = 0; | 142 | length = 0; |
| @@ -158,20 +161,26 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l | |||
| 158 | log_debug_msg("to based on the packet.\n"); | 161 | log_debug_msg("to based on the packet.\n"); |
| 159 | log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); | 162 | log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); |
| 160 | log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length); | 163 | log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length); |
| 161 | return -1; | 164 | return AFC_E_INTERNAL_ERROR; |
| 162 | } | 165 | } |
| 163 | 166 | ||
| 167 | /* send AFC packet header */ | ||
| 168 | AFCPacket_to_LE(client->afc_packet); | ||
| 169 | sent = 0; | ||
| 164 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); | 170 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); |
| 165 | if (sent == 0) { | 171 | if (sent == 0) { |
| 166 | return bytes; | 172 | /* FIXME: should this be handled as success?! */ |
| 173 | return AFC_E_SUCCESS; | ||
| 167 | } | 174 | } |
| 168 | bytes += sent; | 175 | *bytes_sent += sent; |
| 169 | 176 | ||
| 177 | /* send AFC packet data */ | ||
| 178 | sent = 0; | ||
| 170 | iphone_device_send(client->connection, data, offset, &sent); | 179 | iphone_device_send(client->connection, data, offset, &sent); |
| 171 | if (sent == 0) { | 180 | if (sent == 0) { |
| 172 | return bytes; | 181 | return AFC_E_SUCCESS; |
| 173 | } | 182 | } |
| 174 | bytes += sent; | 183 | *bytes_sent += sent; |
| 175 | 184 | ||
| 176 | log_debug_msg("%s: sent the first now go with the second\n", __func__); | 185 | log_debug_msg("%s: sent the first now go with the second\n", __func__); |
| 177 | log_debug_msg("%s: Length: %i\n", __func__, length - offset); | 186 | log_debug_msg("%s: Length: %i\n", __func__, length - offset); |
| @@ -181,8 +190,8 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l | |||
| 181 | sent = 0; | 190 | sent = 0; |
| 182 | iphone_device_send(client->connection, data + offset, length - offset, &sent); | 191 | iphone_device_send(client->connection, data + offset, length - offset, &sent); |
| 183 | 192 | ||
| 184 | bytes = sent; | 193 | *bytes_sent = sent; |
| 185 | return bytes; | 194 | return AFC_E_SUCCESS; |
| 186 | } else { | 195 | } else { |
| 187 | log_debug_msg("%s: doin things the old way\n", __func__); | 196 | log_debug_msg("%s: doin things the old way\n", __func__); |
| 188 | log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); | 197 | log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); |
| @@ -190,35 +199,38 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l | |||
| 190 | log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); | 199 | log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); |
| 191 | log_debug_msg("\n"); | 200 | log_debug_msg("\n"); |
| 192 | 201 | ||
| 202 | /* send AFC packet header */ | ||
| 203 | AFCPacket_to_LE(client->afc_packet); | ||
| 204 | sent = 0; | ||
| 193 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); | 205 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); |
| 194 | if (sent == 0) { | 206 | if (sent == 0) { |
| 195 | return bytes; | 207 | return AFC_E_SUCCESS; |
| 196 | } | 208 | } |
| 197 | bytes += sent; | 209 | *bytes_sent += sent; |
| 210 | /* send AFC packet data (if there's data to send) */ | ||
| 198 | if (length > 0) { | 211 | if (length > 0) { |
| 199 | log_debug_msg("%s: packet data follows\n", __func__); | 212 | log_debug_msg("%s: packet data follows\n", __func__); |
| 200 | 213 | ||
| 201 | log_debug_buffer(data, length); | 214 | log_debug_buffer(data, length); |
| 202 | log_debug_msg("\n"); | 215 | log_debug_msg("\n"); |
| 203 | iphone_device_send(client->connection, data, length, &sent); | 216 | iphone_device_send(client->connection, data, length, &sent); |
| 204 | bytes += sent; | 217 | *bytes_sent += sent; |
| 205 | } | 218 | } |
| 206 | return bytes; | 219 | return AFC_E_SUCCESS; |
| 207 | } | 220 | } |
| 208 | return -1; | 221 | return AFC_E_INTERNAL_ERROR; |
| 209 | } | 222 | } |
| 210 | 223 | ||
| 211 | /** Receives data through an AFC client and sets a variable to the received data. | 224 | /** Receives data through an AFC client and sets a variable to the received data. |
| 212 | * | 225 | * |
| 213 | * @param client The client to receive data on. | 226 | * @param client The client to receive data on. |
| 214 | * @param dump_here The char* to point to the newly-received data. | 227 | * @param dump_here The char* to point to the newly-received data. |
| 228 | * @param bytes_recv How much data was received. | ||
| 215 | * | 229 | * |
| 216 | * @return How much data was received, 0 on successful receive with no errors, | 230 | * @return AFC_E_SUCCESS when data has been received, or an AFC_E_* error value |
| 217 | * -1 if there was an error involved with receiving or if the packet | 231 | * when an error occured. |
| 218 | * received raised a non-trivial error condition (i.e. non-zero with | ||
| 219 | * AFC_ERROR operation) | ||
| 220 | */ | 232 | */ |
| 221 | static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *bytes) | 233 | static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint32_t *bytes_recv) |
| 222 | { | 234 | { |
| 223 | AFCPacket header; | 235 | AFCPacket header; |
| 224 | uint32_t entire_len = 0; | 236 | uint32_t entire_len = 0; |
| @@ -226,15 +238,16 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 226 | uint32_t current_count = 0; | 238 | uint32_t current_count = 0; |
| 227 | uint64_t param1 = -1; | 239 | uint64_t param1 = -1; |
| 228 | 240 | ||
| 229 | *bytes = 0; | 241 | *bytes_recv = 0; |
| 230 | 242 | ||
| 231 | /* first, read the AFC header */ | 243 | /* first, read the AFC header */ |
| 232 | iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); | 244 | iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv); |
| 233 | if (*bytes <= 0) { | 245 | AFCPacket_from_LE(&header); |
| 246 | if (*bytes_recv == 0) { | ||
| 234 | log_debug_msg("%s: Just didn't get enough.\n", __func__); | 247 | log_debug_msg("%s: Just didn't get enough.\n", __func__); |
| 235 | *dump_here = NULL; | 248 | *dump_here = NULL; |
| 236 | return AFC_E_MUX_ERROR; | 249 | return AFC_E_MUX_ERROR; |
| 237 | } else if ((uint32_t)*bytes < sizeof(AFCPacket)) { | 250 | } else if (*bytes_recv < sizeof(AFCPacket)) { |
| 238 | log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); | 251 | log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); |
| 239 | *dump_here = NULL; | 252 | *dump_here = NULL; |
| 240 | return AFC_E_MUX_ERROR; | 253 | return AFC_E_MUX_ERROR; |
| @@ -262,7 +275,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 262 | && header.entire_length == sizeof(AFCPacket)) { | 275 | && header.entire_length == sizeof(AFCPacket)) { |
| 263 | log_debug_msg("%s: Empty AFCPacket received!\n", __func__); | 276 | log_debug_msg("%s: Empty AFCPacket received!\n", __func__); |
| 264 | *dump_here = NULL; | 277 | *dump_here = NULL; |
| 265 | *bytes = 0; | 278 | *bytes_recv = 0; |
| 266 | if (header.operation == AFC_OP_DATA) { | 279 | if (header.operation == AFC_OP_DATA) { |
| 267 | return AFC_E_SUCCESS; | 280 | return AFC_E_SUCCESS; |
| 268 | } else { | 281 | } else { |
| @@ -282,13 +295,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 282 | 295 | ||
| 283 | *dump_here = (char*)malloc(entire_len); | 296 | *dump_here = (char*)malloc(entire_len); |
| 284 | if (this_len > 0) { | 297 | if (this_len > 0) { |
| 285 | iphone_device_recv(client->connection, *dump_here, this_len, (uint32_t*)bytes); | 298 | iphone_device_recv(client->connection, *dump_here, this_len, bytes_recv); |
| 286 | if (*bytes <= 0) { | 299 | if (*bytes_recv <= 0) { |
| 287 | free(*dump_here); | 300 | free(*dump_here); |
| 288 | *dump_here = NULL; | 301 | *dump_here = NULL; |
| 289 | log_debug_msg("%s: Did not get packet contents!\n", __func__); | 302 | log_debug_msg("%s: Did not get packet contents!\n", __func__); |
| 290 | return AFC_E_NOT_ENOUGH_DATA; | 303 | return AFC_E_NOT_ENOUGH_DATA; |
| 291 | } else if ((uint32_t)*bytes < this_len) { | 304 | } else if (*bytes_recv < this_len) { |
| 292 | free(*dump_here); | 305 | free(*dump_here); |
| 293 | *dump_here = NULL; | 306 | *dump_here = NULL; |
| 294 | log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); | 307 | log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); |
| @@ -300,12 +313,12 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 300 | 313 | ||
| 301 | if (entire_len > this_len) { | 314 | if (entire_len > this_len) { |
| 302 | while (current_count < entire_len) { | 315 | while (current_count < entire_len) { |
| 303 | iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes); | 316 | iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv); |
| 304 | if (*bytes <= 0) { | 317 | if (*bytes_recv <= 0) { |
| 305 | log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes); | 318 | log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes_recv); |
| 306 | break; | 319 | break; |
| 307 | } | 320 | } |
| 308 | current_count += *bytes; | 321 | current_count += *bytes_recv; |
| 309 | } | 322 | } |
| 310 | if (current_count < entire_len) { | 323 | if (current_count < entire_len) { |
| 311 | log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); | 324 | log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); |
| @@ -345,7 +358,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 345 | /* unknown operation code received */ | 358 | /* unknown operation code received */ |
| 346 | free(*dump_here); | 359 | free(*dump_here); |
| 347 | *dump_here = NULL; | 360 | *dump_here = NULL; |
| 348 | *bytes = 0; | 361 | *bytes_recv = 0; |
| 349 | 362 | ||
| 350 | log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); | 363 | log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); |
| 351 | fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); | 364 | fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); |
| @@ -353,13 +366,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 353 | return AFC_E_OP_NOT_SUPPORTED; | 366 | return AFC_E_OP_NOT_SUPPORTED; |
| 354 | } | 367 | } |
| 355 | 368 | ||
| 356 | *bytes = current_count; | 369 | *bytes_recv = current_count; |
| 357 | return AFC_E_SUCCESS; | 370 | return AFC_E_SUCCESS; |
| 358 | } | 371 | } |
| 359 | 372 | ||
| 360 | static int count_nullspaces(char *string, int number) | 373 | static uint32_t count_nullspaces(char *string, uint32_t number) |
| 361 | { | 374 | { |
| 362 | int i = 0, nulls = 0; | 375 | uint32_t i = 0, nulls = 0; |
| 363 | 376 | ||
| 364 | for (i = 0; i < number; i++) { | 377 | for (i = 0; i < number; i++) { |
| 365 | if (string[i] == '\0') | 378 | if (string[i] == '\0') |
| @@ -369,9 +382,9 @@ static int count_nullspaces(char *string, int number) | |||
| 369 | return nulls; | 382 | return nulls; |
| 370 | } | 383 | } |
| 371 | 384 | ||
| 372 | static char **make_strings_list(char *tokens, int true_length) | 385 | static char **make_strings_list(char *tokens, uint32_t true_length) |
| 373 | { | 386 | { |
| 374 | int nulls = 0, i = 0, j = 0; | 387 | uint32_t nulls = 0, i = 0, j = 0; |
| 375 | char **list = NULL; | 388 | char **list = NULL; |
| 376 | 389 | ||
| 377 | if (!tokens || !true_length) | 390 | if (!tokens || !true_length) |
| @@ -398,7 +411,7 @@ static char **make_strings_list(char *tokens, int true_length) | |||
| 398 | */ | 411 | */ |
| 399 | afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list) | 412 | afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list) |
| 400 | { | 413 | { |
| 401 | int bytes = 0; | 414 | uint32_t bytes = 0; |
| 402 | char *data = NULL, **list_loc = NULL; | 415 | char *data = NULL, **list_loc = NULL; |
| 403 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 416 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 404 | 417 | ||
| @@ -411,8 +424,8 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis | |||
| 411 | client->afc_packet->operation = AFC_OP_READ_DIR; | 424 | client->afc_packet->operation = AFC_OP_READ_DIR; |
| 412 | client->afc_packet->entire_length = 0; | 425 | client->afc_packet->entire_length = 0; |
| 413 | client->afc_packet->this_length = 0; | 426 | client->afc_packet->this_length = 0; |
| 414 | bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); | 427 | ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes); |
| 415 | if (bytes <= 0) { | 428 | if (ret != AFC_E_SUCCESS) { |
| 416 | afc_unlock(client); | 429 | afc_unlock(client); |
| 417 | return AFC_E_NOT_ENOUGH_DATA; | 430 | return AFC_E_NOT_ENOUGH_DATA; |
| 418 | } | 431 | } |
| @@ -442,7 +455,7 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis | |||
| 442 | */ | 455 | */ |
| 443 | afc_error_t afc_get_device_info(afc_client_t client, char ***infos) | 456 | afc_error_t afc_get_device_info(afc_client_t client, char ***infos) |
| 444 | { | 457 | { |
| 445 | int bytes = 0; | 458 | uint32_t bytes = 0; |
| 446 | char *data = NULL, **list = NULL; | 459 | char *data = NULL, **list = NULL; |
| 447 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 460 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 448 | 461 | ||
| @@ -454,8 +467,8 @@ afc_error_t afc_get_device_info(afc_client_t client, char ***infos) | |||
| 454 | // Send the command | 467 | // Send the command |
| 455 | client->afc_packet->operation = AFC_OP_GET_DEVINFO; | 468 | client->afc_packet->operation = AFC_OP_GET_DEVINFO; |
| 456 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 469 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 457 | bytes = afc_dispatch_packet(client, NULL, 0); | 470 | ret = afc_dispatch_packet(client, NULL, 0, &bytes); |
| 458 | if (bytes < 0) { | 471 | if (ret != AFC_E_SUCCESS) { |
| 459 | afc_unlock(client); | 472 | afc_unlock(client); |
| 460 | return AFC_E_NOT_ENOUGH_DATA; | 473 | return AFC_E_NOT_ENOUGH_DATA; |
| 461 | } | 474 | } |
| @@ -523,7 +536,7 @@ afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char * | |||
| 523 | afc_error_t afc_remove_path(afc_client_t client, const char *path) | 536 | afc_error_t afc_remove_path(afc_client_t client, const char *path) |
| 524 | { | 537 | { |
| 525 | char *response = NULL; | 538 | char *response = NULL; |
| 526 | int bytes; | 539 | uint32_t bytes = 0; |
| 527 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 540 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 528 | 541 | ||
| 529 | if (!client || !path || !client->afc_packet || !client->connection) | 542 | if (!client || !path || !client->afc_packet || !client->connection) |
| @@ -534,8 +547,8 @@ afc_error_t afc_remove_path(afc_client_t client, const char *path) | |||
| 534 | // Send command | 547 | // Send command |
| 535 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 548 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 536 | client->afc_packet->operation = AFC_OP_REMOVE_PATH; | 549 | client->afc_packet->operation = AFC_OP_REMOVE_PATH; |
| 537 | bytes = afc_dispatch_packet(client, path, strlen(path)+1); | 550 | ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes); |
| 538 | if (bytes <= 0) { | 551 | if (ret != AFC_E_SUCCESS) { |
| 539 | afc_unlock(client); | 552 | afc_unlock(client); |
| 540 | return AFC_E_NOT_ENOUGH_DATA; | 553 | return AFC_E_NOT_ENOUGH_DATA; |
| 541 | } | 554 | } |
| @@ -566,7 +579,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t | |||
| 566 | { | 579 | { |
| 567 | char *response = NULL; | 580 | char *response = NULL; |
| 568 | char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); | 581 | char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); |
| 569 | int bytes = 0; | 582 | uint32_t bytes = 0; |
| 570 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 583 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 571 | 584 | ||
| 572 | if (!client || !from || !to || !client->afc_packet || !client->connection) | 585 | if (!client || !from || !to || !client->afc_packet || !client->connection) |
| @@ -579,9 +592,9 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t | |||
| 579 | memcpy(send + strlen(from) + 1, to, strlen(to) + 1); | 592 | memcpy(send + strlen(from) + 1, to, strlen(to) + 1); |
| 580 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 593 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 581 | client->afc_packet->operation = AFC_OP_RENAME_PATH; | 594 | client->afc_packet->operation = AFC_OP_RENAME_PATH; |
| 582 | bytes = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1); | 595 | ret = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1, &bytes); |
| 583 | free(send); | 596 | free(send); |
| 584 | if (bytes <= 0) { | 597 | if (ret != AFC_E_SUCCESS) { |
| 585 | afc_unlock(client); | 598 | afc_unlock(client); |
| 586 | return AFC_E_NOT_ENOUGH_DATA; | 599 | return AFC_E_NOT_ENOUGH_DATA; |
| 587 | } | 600 | } |
| @@ -606,7 +619,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t | |||
| 606 | */ | 619 | */ |
| 607 | afc_error_t afc_make_directory(afc_client_t client, const char *dir) | 620 | afc_error_t afc_make_directory(afc_client_t client, const char *dir) |
| 608 | { | 621 | { |
| 609 | int bytes = 0; | 622 | uint32_t bytes = 0; |
| 610 | char *response = NULL; | 623 | char *response = NULL; |
| 611 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 624 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 612 | 625 | ||
| @@ -618,8 +631,8 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir) | |||
| 618 | // Send command | 631 | // Send command |
| 619 | client->afc_packet->operation = AFC_OP_MAKE_DIR; | 632 | client->afc_packet->operation = AFC_OP_MAKE_DIR; |
| 620 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 633 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 621 | bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); | 634 | ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes); |
| 622 | if (bytes <= 0) { | 635 | if (ret != AFC_E_SUCCESS) { |
| 623 | afc_unlock(client); | 636 | afc_unlock(client); |
| 624 | return AFC_E_NOT_ENOUGH_DATA; | 637 | return AFC_E_NOT_ENOUGH_DATA; |
| 625 | } | 638 | } |
| @@ -647,7 +660,7 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir) | |||
| 647 | afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) | 660 | afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) |
| 648 | { | 661 | { |
| 649 | char *received = NULL; | 662 | char *received = NULL; |
| 650 | int bytes; | 663 | uint32_t bytes = 0; |
| 651 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 664 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 652 | 665 | ||
| 653 | if (!client || !path || !infolist) | 666 | if (!client || !path || !infolist) |
| @@ -658,7 +671,11 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***inf | |||
| 658 | // Send command | 671 | // Send command |
| 659 | client->afc_packet->operation = AFC_OP_GET_FILE_INFO; | 672 | client->afc_packet->operation = AFC_OP_GET_FILE_INFO; |
| 660 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 673 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 661 | afc_dispatch_packet(client, path, strlen(path)+1); | 674 | ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes); |
| 675 | if (ret != AFC_E_SUCCESS) { | ||
| 676 | afc_unlock(client); | ||
| 677 | return AFC_E_NOT_ENOUGH_DATA; | ||
| 678 | } | ||
| 662 | 679 | ||
| 663 | // Receive data | 680 | // Receive data |
| 664 | ret = afc_receive_data(client, &received, &bytes); | 681 | ret = afc_receive_data(client, &received, &bytes); |
| @@ -688,8 +705,8 @@ iphone_error_t | |||
| 688 | afc_file_open(afc_client_t client, const char *filename, | 705 | afc_file_open(afc_client_t client, const char *filename, |
| 689 | afc_file_mode_t file_mode, uint64_t *handle) | 706 | afc_file_mode_t file_mode, uint64_t *handle) |
| 690 | { | 707 | { |
| 691 | uint32_t ag = 0; | 708 | uint64_t file_mode_loc = GUINT64_TO_LE(file_mode); |
| 692 | int bytes = 0; | 709 | uint32_t bytes = 0; |
| 693 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); | 710 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); |
| 694 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 711 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 695 | 712 | ||
| @@ -702,16 +719,15 @@ afc_file_open(afc_client_t client, const char *filename, | |||
| 702 | afc_lock(client); | 719 | afc_lock(client); |
| 703 | 720 | ||
| 704 | // Send command | 721 | // Send command |
| 705 | memcpy(data, &file_mode, 4); | 722 | memcpy(data, &file_mode_loc, 8); |
| 706 | memcpy(data + 4, &ag, 4); | ||
| 707 | memcpy(data + 8, filename, strlen(filename)); | 723 | memcpy(data + 8, filename, strlen(filename)); |
| 708 | data[8 + strlen(filename)] = '\0'; | 724 | data[8 + strlen(filename)] = '\0'; |
| 709 | client->afc_packet->operation = AFC_OP_FILE_OPEN; | 725 | client->afc_packet->operation = AFC_OP_FILE_OPEN; |
| 710 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 726 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 711 | bytes = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1); | 727 | ret = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1, &bytes); |
| 712 | free(data); | 728 | free(data); |
| 713 | 729 | ||
| 714 | if (bytes <= 0) { | 730 | if (ret != AFC_E_SUCCESS) { |
| 715 | log_debug_msg("%s: Didn't receive a response to the command\n", __func__); | 731 | log_debug_msg("%s: Didn't receive a response to the command\n", __func__); |
| 716 | afc_unlock(client); | 732 | afc_unlock(client); |
| 717 | return AFC_E_NOT_ENOUGH_DATA; | 733 | return AFC_E_NOT_ENOUGH_DATA; |
| @@ -740,18 +756,19 @@ afc_file_open(afc_client_t client, const char *filename, | |||
| 740 | * @param handle File handle of a previously opened file | 756 | * @param handle File handle of a previously opened file |
| 741 | * @param data The pointer to the memory region to store the read data | 757 | * @param data The pointer to the memory region to store the read data |
| 742 | * @param length The number of bytes to read | 758 | * @param length The number of bytes to read |
| 759 | * @param bytes_read The number of bytes actually read. | ||
| 743 | * | 760 | * |
| 744 | * @return The number of bytes read if successful. If there was an error -1. | 761 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value on error. |
| 745 | */ | 762 | */ |
| 746 | iphone_error_t | 763 | iphone_error_t |
| 747 | afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) | 764 | afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read) |
| 748 | { | 765 | { |
| 749 | char *input = NULL; | 766 | char *input = NULL; |
| 750 | int current_count = 0, bytes_loc = 0; | 767 | uint32_t current_count = 0, bytes_loc = 0; |
| 751 | const int MAXIMUM_READ_SIZE = 1 << 16; | 768 | const uint32_t MAXIMUM_READ_SIZE = 1 << 16; |
| 752 | afc_error_t ret = AFC_E_SUCCESS; | 769 | afc_error_t ret = AFC_E_SUCCESS; |
| 753 | 770 | ||
| 754 | if (!client || !client->afc_packet || !client->connection || handle == 0 || (length < 0)) | 771 | if (!client || !client->afc_packet || !client->connection || handle == 0) |
| 755 | return AFC_E_INVALID_ARGUMENT; | 772 | return AFC_E_INVALID_ARGUMENT; |
| 756 | log_debug_msg("%s: called for length %i\n", __func__, length); | 773 | log_debug_msg("%s: called for length %i\n", __func__, length); |
| 757 | 774 | ||
| @@ -765,13 +782,13 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint | |||
| 765 | // Send the read command | 782 | // Send the read command |
| 766 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); | 783 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); |
| 767 | packet->filehandle = handle; | 784 | packet->filehandle = handle; |
| 768 | packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; | 785 | packet->size = GUINT64_TO_LE(((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE); |
| 769 | client->afc_packet->operation = AFC_OP_READ; | 786 | client->afc_packet->operation = AFC_OP_READ; |
| 770 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 787 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 771 | bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); | 788 | ret = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket), &bytes_loc); |
| 772 | free(packet); | 789 | free(packet); |
| 773 | 790 | ||
| 774 | if (bytes_loc <= 0) { | 791 | if (ret != AFC_E_SUCCESS) { |
| 775 | afc_unlock(client); | 792 | afc_unlock(client); |
| 776 | return AFC_E_NOT_ENOUGH_DATA; | 793 | return AFC_E_NOT_ENOUGH_DATA; |
| 777 | } | 794 | } |
| @@ -786,7 +803,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint | |||
| 786 | if (input) | 803 | if (input) |
| 787 | free(input); | 804 | free(input); |
| 788 | afc_unlock(client); | 805 | afc_unlock(client); |
| 789 | *bytes = current_count; | 806 | *bytes_read = current_count; |
| 790 | /* FIXME: check that's actually a success */ | 807 | /* FIXME: check that's actually a success */ |
| 791 | return ret; | 808 | return ret; |
| 792 | } else { | 809 | } else { |
| @@ -802,7 +819,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint | |||
| 802 | log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); | 819 | log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); |
| 803 | 820 | ||
| 804 | afc_unlock(client); | 821 | afc_unlock(client); |
| 805 | *bytes = current_count; | 822 | *bytes_read = current_count; |
| 806 | return ret; | 823 | return ret; |
| 807 | } | 824 | } |
| 808 | 825 | ||
| @@ -812,23 +829,22 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint | |||
| 812 | * @param handle File handle of previously opened file. | 829 | * @param handle File handle of previously opened file. |
| 813 | * @param data The data to write to the file. | 830 | * @param data The data to write to the file. |
| 814 | * @param length How much data to write. | 831 | * @param length How much data to write. |
| 832 | * @param bytes_written The number of bytes actually written to the file. | ||
| 815 | * | 833 | * |
| 816 | * @return The number of bytes written to the file, or a value less than 0 if | 834 | * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error. |
| 817 | * none were written... | ||
| 818 | */ | 835 | */ |
| 819 | iphone_error_t | 836 | iphone_error_t |
| 820 | afc_file_write(afc_client_t client, uint64_t handle, | 837 | afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written) |
| 821 | const char *data, int length, uint32_t * bytes) | ||
| 822 | { | 838 | { |
| 823 | char *acknowledgement = NULL; | 839 | char *acknowledgement = NULL; |
| 824 | const int MAXIMUM_WRITE_SIZE = 1 << 15; | 840 | const uint32_t MAXIMUM_WRITE_SIZE = 1 << 15; |
| 825 | uint32_t zero = 0, current_count = 0, i = 0; | 841 | uint32_t current_count = 0, i = 0; |
| 826 | uint32_t segments = (length / MAXIMUM_WRITE_SIZE); | 842 | uint32_t segments = (length / MAXIMUM_WRITE_SIZE); |
| 827 | int bytes_loc = 0; | 843 | uint32_t bytes_loc = 0; |
| 828 | char *out_buffer = NULL; | 844 | char *out_buffer = NULL; |
| 829 | afc_error_t ret = AFC_E_SUCCESS; | 845 | afc_error_t ret = AFC_E_SUCCESS; |
| 830 | 846 | ||
| 831 | if (!client || !client->afc_packet || !client->connection || !bytes || (handle == 0) || (length < 0)) | 847 | if (!client || !client->afc_packet || !client->connection || !bytes_written || (handle == 0)) |
| 832 | return AFC_E_INVALID_ARGUMENT; | 848 | return AFC_E_INVALID_ARGUMENT; |
| 833 | 849 | ||
| 834 | afc_lock(client); | 850 | afc_lock(client); |
| @@ -844,8 +860,8 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 844 | out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); | 860 | out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); |
| 845 | memcpy(out_buffer, (char *)&handle, sizeof(uint64_t)); | 861 | memcpy(out_buffer, (char *)&handle, sizeof(uint64_t)); |
| 846 | memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); | 862 | memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); |
| 847 | bytes_loc = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); | 863 | ret = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8, &bytes_loc); |
| 848 | if (bytes_loc < 0) { | 864 | if (ret != AFC_E_SUCCESS) { |
| 849 | afc_unlock(client); | 865 | afc_unlock(client); |
| 850 | return AFC_E_NOT_ENOUGH_DATA; | 866 | return AFC_E_NOT_ENOUGH_DATA; |
| 851 | } | 867 | } |
| @@ -866,9 +882,9 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 866 | // didn't get sent in the for loop | 882 | // didn't get sent in the for loop |
| 867 | // this length is fine because it's always sizeof(AFCPacket) + 8, but | 883 | // this length is fine because it's always sizeof(AFCPacket) + 8, but |
| 868 | // to be sure we do it again | 884 | // to be sure we do it again |
| 869 | if (current_count == (uint32_t)length) { | 885 | if (current_count == length) { |
| 870 | afc_unlock(client); | 886 | afc_unlock(client); |
| 871 | *bytes = current_count; | 887 | *bytes_written = current_count; |
| 872 | return ret; | 888 | return ret; |
| 873 | } | 889 | } |
| 874 | 890 | ||
| @@ -878,19 +894,18 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 878 | out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); | 894 | out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); |
| 879 | memcpy(out_buffer, (char *) &handle, sizeof(uint64_t)); | 895 | memcpy(out_buffer, (char *) &handle, sizeof(uint64_t)); |
| 880 | memcpy(out_buffer + 8, data + current_count, (length - current_count)); | 896 | memcpy(out_buffer + 8, data + current_count, (length - current_count)); |
| 881 | bytes_loc = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8); | 897 | ret = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8, &bytes_loc); |
| 882 | free(out_buffer); | 898 | free(out_buffer); |
| 883 | out_buffer = NULL; | 899 | out_buffer = NULL; |
| 884 | 900 | ||
| 885 | current_count += bytes_loc; | 901 | current_count += bytes_loc; |
| 886 | 902 | ||
| 887 | if (bytes_loc <= 0) { | 903 | if (ret != AFC_E_SUCCESS) { |
| 888 | afc_unlock(client); | 904 | afc_unlock(client); |
| 889 | *bytes = current_count; | 905 | *bytes_written = current_count; |
| 890 | return AFC_E_SUCCESS; | 906 | return AFC_E_SUCCESS; |
| 891 | } | 907 | } |
| 892 | 908 | ||
| 893 | zero = bytes_loc; | ||
| 894 | ret = afc_receive_data(client, &acknowledgement, &bytes_loc); | 909 | ret = afc_receive_data(client, &acknowledgement, &bytes_loc); |
| 895 | afc_unlock(client); | 910 | afc_unlock(client); |
| 896 | if (ret != AFC_E_SUCCESS) { | 911 | if (ret != AFC_E_SUCCESS) { |
| @@ -898,7 +913,7 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 898 | } else { | 913 | } else { |
| 899 | free(acknowledgement); | 914 | free(acknowledgement); |
| 900 | } | 915 | } |
| 901 | *bytes = current_count; | 916 | *bytes_written = current_count; |
| 902 | return ret; | 917 | return ret; |
| 903 | } | 918 | } |
| 904 | 919 | ||
| @@ -910,7 +925,7 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 910 | afc_error_t afc_file_close(afc_client_t client, uint64_t handle) | 925 | afc_error_t afc_file_close(afc_client_t client, uint64_t handle) |
| 911 | { | 926 | { |
| 912 | char *buffer = malloc(sizeof(char) * 8); | 927 | char *buffer = malloc(sizeof(char) * 8); |
| 913 | int bytes = 0; | 928 | uint32_t bytes = 0; |
| 914 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 929 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 915 | 930 | ||
| 916 | if (!client || (handle == 0)) | 931 | if (!client || (handle == 0)) |
| @@ -924,11 +939,11 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle) | |||
| 924 | memcpy(buffer, &handle, sizeof(uint64_t)); | 939 | memcpy(buffer, &handle, sizeof(uint64_t)); |
| 925 | client->afc_packet->operation = AFC_OP_FILE_CLOSE; | 940 | client->afc_packet->operation = AFC_OP_FILE_CLOSE; |
| 926 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 941 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 927 | bytes = afc_dispatch_packet(client, buffer, 8); | 942 | ret = afc_dispatch_packet(client, buffer, 8, &bytes); |
| 928 | free(buffer); | 943 | free(buffer); |
| 929 | buffer = NULL; | 944 | buffer = NULL; |
| 930 | 945 | ||
| 931 | if (bytes <= 0) { | 946 | if (ret != AFC_E_SUCCESS) { |
| 932 | afc_unlock(client); | 947 | afc_unlock(client); |
| 933 | return AFC_E_UNKNOWN_ERROR; | 948 | return AFC_E_UNKNOWN_ERROR; |
| 934 | } | 949 | } |
| @@ -957,8 +972,8 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle) | |||
| 957 | afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) | 972 | afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) |
| 958 | { | 973 | { |
| 959 | char *buffer = malloc(16); | 974 | char *buffer = malloc(16); |
| 960 | int bytes = 0; | 975 | uint32_t bytes = 0; |
| 961 | uint64_t op = operation; | 976 | uint64_t op = GUINT64_TO_LE(operation); |
| 962 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 977 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 963 | 978 | ||
| 964 | if (!client || (handle == 0)) | 979 | if (!client || (handle == 0)) |
| @@ -974,11 +989,11 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op | |||
| 974 | 989 | ||
| 975 | client->afc_packet->operation = AFC_OP_FILE_LOCK; | 990 | client->afc_packet->operation = AFC_OP_FILE_LOCK; |
| 976 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 991 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 977 | bytes = afc_dispatch_packet(client, buffer, 16); | 992 | ret = afc_dispatch_packet(client, buffer, 16, &bytes); |
| 978 | free(buffer); | 993 | free(buffer); |
| 979 | buffer = NULL; | 994 | buffer = NULL; |
| 980 | 995 | ||
| 981 | if (bytes <= 0) { | 996 | if (ret != AFC_E_SUCCESS) { |
| 982 | afc_unlock(client); | 997 | afc_unlock(client); |
| 983 | log_debug_msg("%s: could not send lock command\n", __func__); | 998 | log_debug_msg("%s: could not send lock command\n", __func__); |
| 984 | return AFC_E_UNKNOWN_ERROR; | 999 | return AFC_E_UNKNOWN_ERROR; |
| @@ -1006,8 +1021,9 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op | |||
| 1006 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) | 1021 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) |
| 1007 | { | 1022 | { |
| 1008 | char *buffer = (char *) malloc(sizeof(char) * 24); | 1023 | char *buffer = (char *) malloc(sizeof(char) * 24); |
| 1009 | uint32_t zero = 0; | 1024 | int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset); |
| 1010 | int bytes = 0; | 1025 | uint64_t whence_loc = GUINT64_TO_LE(whence); |
| 1026 | uint32_t bytes = 0; | ||
| 1011 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1027 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1012 | 1028 | ||
| 1013 | if (!client || (handle == 0)) | 1029 | if (!client || (handle == 0)) |
| @@ -1017,16 +1033,15 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, | |||
| 1017 | 1033 | ||
| 1018 | // Send the command | 1034 | // Send the command |
| 1019 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle | 1035 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle |
| 1020 | memcpy(buffer + 8, &whence, sizeof(int32_t)); // fromwhere | 1036 | memcpy(buffer + 8, &whence_loc, sizeof(uint64_t)); // fromwhere |
| 1021 | memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad | 1037 | memcpy(buffer + 16, &offset_loc, sizeof(uint64_t)); // offset |
| 1022 | memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset | ||
| 1023 | client->afc_packet->operation = AFC_OP_FILE_SEEK; | 1038 | client->afc_packet->operation = AFC_OP_FILE_SEEK; |
| 1024 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 1039 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 1025 | bytes = afc_dispatch_packet(client, buffer, 24); | 1040 | ret = afc_dispatch_packet(client, buffer, 24, &bytes); |
| 1026 | free(buffer); | 1041 | free(buffer); |
| 1027 | buffer = NULL; | 1042 | buffer = NULL; |
| 1028 | 1043 | ||
| 1029 | if (bytes <= 0) { | 1044 | if (ret != AFC_E_SUCCESS) { |
| 1030 | afc_unlock(client); | 1045 | afc_unlock(client); |
| 1031 | return AFC_E_NOT_ENOUGH_DATA; | 1046 | return AFC_E_NOT_ENOUGH_DATA; |
| 1032 | } | 1047 | } |
| @@ -1051,7 +1066,7 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, | |||
| 1051 | afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) | 1066 | afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) |
| 1052 | { | 1067 | { |
| 1053 | char *buffer = (char *) malloc(sizeof(char) * 8); | 1068 | char *buffer = (char *) malloc(sizeof(char) * 8); |
| 1054 | int bytes = 0; | 1069 | uint32_t bytes = 0; |
| 1055 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1070 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1056 | 1071 | ||
| 1057 | if (!client || (handle == 0)) | 1072 | if (!client || (handle == 0)) |
| @@ -1063,11 +1078,11 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi | |||
| 1063 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle | 1078 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle |
| 1064 | client->afc_packet->operation = AFC_OP_FILE_TELL; | 1079 | client->afc_packet->operation = AFC_OP_FILE_TELL; |
| 1065 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 1080 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 1066 | bytes = afc_dispatch_packet(client, buffer, 8); | 1081 | ret = afc_dispatch_packet(client, buffer, 8, &bytes); |
| 1067 | free(buffer); | 1082 | free(buffer); |
| 1068 | buffer = NULL; | 1083 | buffer = NULL; |
| 1069 | 1084 | ||
| 1070 | if (bytes <= 0) { | 1085 | if (ret != AFC_E_SUCCESS) { |
| 1071 | afc_unlock(client); | 1086 | afc_unlock(client); |
| 1072 | return AFC_E_NOT_ENOUGH_DATA; | 1087 | return AFC_E_NOT_ENOUGH_DATA; |
| 1073 | } | 1088 | } |
| @@ -1077,6 +1092,7 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi | |||
| 1077 | if (bytes > 0 && buffer) { | 1092 | if (bytes > 0 && buffer) { |
| 1078 | /* Get the position */ | 1093 | /* Get the position */ |
| 1079 | memcpy(position, buffer, sizeof(uint64_t)); | 1094 | memcpy(position, buffer, sizeof(uint64_t)); |
| 1095 | *position = GUINT64_FROM_LE(*position); | ||
| 1080 | } | 1096 | } |
| 1081 | if (buffer) | 1097 | if (buffer) |
| 1082 | free(buffer); | 1098 | free(buffer); |
| @@ -1100,7 +1116,8 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi | |||
| 1100 | afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) | 1116 | afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) |
| 1101 | { | 1117 | { |
| 1102 | char *buffer = (char *) malloc(sizeof(char) * 16); | 1118 | char *buffer = (char *) malloc(sizeof(char) * 16); |
| 1103 | int bytes = 0; | 1119 | uint32_t bytes = 0; |
| 1120 | uint64_t newsize_loc = GUINT64_TO_LE(newsize); | ||
| 1104 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1121 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1105 | 1122 | ||
| 1106 | if (!client || (handle == 0)) | 1123 | if (!client || (handle == 0)) |
| @@ -1110,14 +1127,14 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new | |||
| 1110 | 1127 | ||
| 1111 | // Send command | 1128 | // Send command |
| 1112 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle | 1129 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle |
| 1113 | memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize | 1130 | memcpy(buffer + 8, &newsize_loc, sizeof(uint64_t)); // newsize |
| 1114 | client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; | 1131 | client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; |
| 1115 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 1132 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 1116 | bytes = afc_dispatch_packet(client, buffer, 16); | 1133 | ret = afc_dispatch_packet(client, buffer, 16, &bytes); |
| 1117 | free(buffer); | 1134 | free(buffer); |
| 1118 | buffer = NULL; | 1135 | buffer = NULL; |
| 1119 | 1136 | ||
| 1120 | if (bytes <= 0) { | 1137 | if (ret != AFC_E_SUCCESS) { |
| 1121 | afc_unlock(client); | 1138 | afc_unlock(client); |
| 1122 | return AFC_E_NOT_ENOUGH_DATA; | 1139 | return AFC_E_NOT_ENOUGH_DATA; |
| 1123 | } | 1140 | } |
| @@ -1140,12 +1157,12 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new | |||
| 1140 | * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT | 1157 | * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT |
| 1141 | * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. | 1158 | * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. |
| 1142 | */ | 1159 | */ |
| 1143 | afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) | 1160 | afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize) |
| 1144 | { | 1161 | { |
| 1145 | char *response = NULL; | 1162 | char *response = NULL; |
| 1146 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); | 1163 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); |
| 1147 | int bytes = 0; | 1164 | uint32_t bytes = 0; |
| 1148 | uint64_t size_requested = newsize; | 1165 | uint64_t size_requested = GUINT64_TO_LE(newsize); |
| 1149 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1166 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1150 | 1167 | ||
| 1151 | if (!client || !path || !client->afc_packet || !client->connection) | 1168 | if (!client || !path || !client->afc_packet || !client->connection) |
| @@ -1158,9 +1175,9 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) | |||
| 1158 | memcpy(send + 8, path, strlen(path) + 1); | 1175 | memcpy(send + 8, path, strlen(path) + 1); |
| 1159 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 1176 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 1160 | client->afc_packet->operation = AFC_OP_TRUNCATE; | 1177 | client->afc_packet->operation = AFC_OP_TRUNCATE; |
| 1161 | bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); | 1178 | ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes); |
| 1162 | free(send); | 1179 | free(send); |
| 1163 | if (bytes <= 0) { | 1180 | if (ret != AFC_E_SUCCESS) { |
| 1164 | afc_unlock(client); | 1181 | afc_unlock(client); |
| 1165 | return AFC_E_NOT_ENOUGH_DATA; | 1182 | return AFC_E_NOT_ENOUGH_DATA; |
| 1166 | } | 1183 | } |
| @@ -1188,8 +1205,8 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c | |||
| 1188 | { | 1205 | { |
| 1189 | char *response = NULL; | 1206 | char *response = NULL; |
| 1190 | char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); | 1207 | char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); |
| 1191 | int bytes = 0; | 1208 | uint32_t bytes = 0; |
| 1192 | uint64_t type = linktype; | 1209 | uint64_t type = GUINT64_TO_LE(linktype); |
| 1193 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1210 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1194 | 1211 | ||
| 1195 | if (!client || !target || !linkname || !client->afc_packet || !client->connection) | 1212 | if (!client || !target || !linkname || !client->afc_packet || !client->connection) |
| @@ -1207,9 +1224,9 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c | |||
| 1207 | memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1); | 1224 | memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1); |
| 1208 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 1225 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 1209 | client->afc_packet->operation = AFC_OP_MAKE_LINK; | 1226 | client->afc_packet->operation = AFC_OP_MAKE_LINK; |
| 1210 | bytes = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); | 1227 | ret = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1, &bytes); |
| 1211 | free(send); | 1228 | free(send); |
| 1212 | if (bytes <= 0) { | 1229 | if (ret != AFC_E_SUCCESS) { |
| 1213 | afc_unlock(client); | 1230 | afc_unlock(client); |
| 1214 | return AFC_E_NOT_ENOUGH_DATA; | 1231 | return AFC_E_NOT_ENOUGH_DATA; |
| 1215 | } | 1232 | } |
| @@ -1236,7 +1253,8 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt | |||
| 1236 | { | 1253 | { |
| 1237 | char *response = NULL; | 1254 | char *response = NULL; |
| 1238 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); | 1255 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); |
| 1239 | int bytes = 0; | 1256 | uint32_t bytes = 0; |
| 1257 | uint64_t mtime_loc = GUINT64_TO_LE(mtime); | ||
| 1240 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1258 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1241 | 1259 | ||
| 1242 | if (!client || !path || !client->afc_packet || !client->connection) | 1260 | if (!client || !path || !client->afc_packet || !client->connection) |
| @@ -1245,13 +1263,13 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt | |||
| 1245 | afc_lock(client); | 1263 | afc_lock(client); |
| 1246 | 1264 | ||
| 1247 | // Send command | 1265 | // Send command |
| 1248 | memcpy(send, &mtime, 8); | 1266 | memcpy(send, &mtime_loc, 8); |
| 1249 | memcpy(send + 8, path, strlen(path) + 1); | 1267 | memcpy(send + 8, path, strlen(path) + 1); |
| 1250 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 1268 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 1251 | client->afc_packet->operation = AFC_OP_SET_FILE_TIME; | 1269 | client->afc_packet->operation = AFC_OP_SET_FILE_TIME; |
| 1252 | bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); | 1270 | ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes); |
| 1253 | free(send); | 1271 | free(send); |
| 1254 | if (bytes <= 0) { | 1272 | if (ret != AFC_E_SUCCESS) { |
| 1255 | afc_unlock(client); | 1273 | afc_unlock(client); |
| 1256 | return AFC_E_NOT_ENOUGH_DATA; | 1274 | return AFC_E_NOT_ENOUGH_DATA; |
| 1257 | } | 1275 | } |
| @@ -36,6 +36,18 @@ typedef struct { | |||
| 36 | uint64_t entire_length, this_length, packet_num, operation; | 36 | uint64_t entire_length, this_length, packet_num, operation; |
| 37 | } AFCPacket; | 37 | } AFCPacket; |
| 38 | 38 | ||
| 39 | #define AFCPacket_to_LE(x) \ | ||
| 40 | (x)->entire_length = GUINT64_TO_LE((x)->entire_length); \ | ||
| 41 | (x)->this_length = GUINT64_TO_LE((x)->this_length); \ | ||
| 42 | (x)->packet_num = GUINT64_TO_LE((x)->packet_num); \ | ||
| 43 | (x)->operation = GUINT64_TO_LE((x)->operation); | ||
| 44 | |||
| 45 | #define AFCPacket_from_LE(x) \ | ||
| 46 | (x)->entire_length = GUINT64_FROM_LE((x)->entire_length); \ | ||
| 47 | (x)->this_length = GUINT64_FROM_LE((x)->this_length); \ | ||
| 48 | (x)->packet_num = GUINT64_FROM_LE((x)->packet_num); \ | ||
| 49 | (x)->operation = GUINT64_FROM_LE((x)->operation); | ||
| 50 | |||
| 39 | typedef struct { | 51 | typedef struct { |
| 40 | uint64_t filehandle, size; | 52 | uint64_t filehandle, size; |
| 41 | } AFCFilePacket; | 53 | } AFCFilePacket; |
diff --git a/src/lockdown.c b/src/lockdown.c index b182706..fb5f8f5 100644 --- a/src/lockdown.c +++ b/src/lockdown.c | |||
| @@ -115,18 +115,23 @@ static int lockdown_check_result(plist_t dict, const char *query_match) | |||
| 115 | * | 115 | * |
| 116 | * @return an error code (LOCKDOWN_E_SUCCESS on success) | 116 | * @return an error code (LOCKDOWN_E_SUCCESS on success) |
| 117 | */ | 117 | */ |
| 118 | lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id) | 118 | lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client) |
| 119 | { | 119 | { |
| 120 | if (!client) | 120 | if (!client) |
| 121 | return LOCKDOWN_E_INVALID_ARG; | 121 | return LOCKDOWN_E_INVALID_ARG; |
| 122 | 122 | ||
| 123 | if (!client->session_id) { | ||
| 124 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: no session_id given, cannot stop session\n", __func__); | ||
| 125 | return LOCKDOWN_E_INVALID_ARG; | ||
| 126 | } | ||
| 127 | |||
| 123 | lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; | 128 | lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 124 | 129 | ||
| 125 | plist_t dict = plist_new_dict(); | 130 | plist_t dict = plist_new_dict(); |
| 126 | plist_dict_insert_item(dict,"Request", plist_new_string("StopSession")); | 131 | plist_dict_insert_item(dict,"Request", plist_new_string("StopSession")); |
| 127 | plist_dict_insert_item(dict,"SessionID", plist_new_string(session_id)); | 132 | plist_dict_insert_item(dict,"SessionID", plist_new_string(client->session_id)); |
| 128 | 133 | ||
| 129 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: called\n", __func__); | 134 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping session %s\n", __func__, client->session_id); |
| 130 | 135 | ||
| 131 | ret = lockdownd_send(client, dict); | 136 | ret = lockdownd_send(client, dict); |
| 132 | 137 | ||
| @@ -148,6 +153,9 @@ lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char * | |||
| 148 | plist_free(dict); | 153 | plist_free(dict); |
| 149 | dict = NULL; | 154 | dict = NULL; |
| 150 | 155 | ||
| 156 | free(client->session_id); | ||
| 157 | client->session_id = NULL; | ||
| 158 | |||
| 151 | return ret; | 159 | return ret; |
| 152 | } | 160 | } |
| 153 | 161 | ||
| @@ -170,7 +178,7 @@ static lockdownd_error_t lockdownd_stop_ssl_session(lockdownd_client_t client) | |||
| 170 | 178 | ||
| 171 | if (client->in_SSL) { | 179 | if (client->in_SSL) { |
| 172 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping SSL session\n", __func__); | 180 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: stopping SSL session\n", __func__); |
| 173 | ret = lockdownd_stop_session(client, client->session_id); | 181 | ret = lockdownd_stop_session(client); |
| 174 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending SSL close notify\n", __func__); | 182 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: sending SSL close notify\n", __func__); |
| 175 | gnutls_bye(client->ssl_session, GNUTLS_SHUT_RDWR); | 183 | gnutls_bye(client->ssl_session, GNUTLS_SHUT_RDWR); |
| 176 | } | 184 | } |
| @@ -209,6 +217,13 @@ lockdownd_error_t lockdownd_client_free(lockdownd_client_t client) | |||
| 209 | } | 217 | } |
| 210 | } | 218 | } |
| 211 | 219 | ||
| 220 | if (client->session_id) { | ||
| 221 | free(client->session_id); | ||
| 222 | } | ||
| 223 | if (client->uuid) { | ||
| 224 | free(client->uuid); | ||
| 225 | } | ||
| 226 | |||
| 212 | free(client); | 227 | free(client); |
| 213 | return ret; | 228 | return ret; |
| 214 | } | 229 | } |
| @@ -642,31 +657,27 @@ lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_ | |||
| 642 | client_loc->ssl_session = NULL; | 657 | client_loc->ssl_session = NULL; |
| 643 | client_loc->ssl_certificate = NULL; | 658 | client_loc->ssl_certificate = NULL; |
| 644 | client_loc->in_SSL = 0; | 659 | client_loc->in_SSL = 0; |
| 660 | client_loc->session_id = NULL; | ||
| 661 | client_loc->uuid = NULL; | ||
| 645 | 662 | ||
| 646 | if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc)) { | 663 | if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc)) { |
| 647 | log_debug_msg("%s: QueryType failed in the lockdownd client.\n", __func__); | 664 | log_debug_msg("%s: QueryType failed in the lockdownd client.\n", __func__); |
| 648 | ret = LOCKDOWN_E_NOT_ENOUGH_DATA; | 665 | ret = LOCKDOWN_E_NOT_ENOUGH_DATA; |
| 649 | } | 666 | } |
| 650 | 667 | ||
| 651 | char *uuid = NULL; | 668 | ret = iphone_device_get_uuid(device, &client_loc->uuid); |
| 652 | ret = iphone_device_get_uuid(device, &uuid); | ||
| 653 | if (LOCKDOWN_E_SUCCESS != ret) { | 669 | if (LOCKDOWN_E_SUCCESS != ret) { |
| 654 | log_debug_msg("%s: failed to get device uuid.\n", __func__); | 670 | log_debug_msg("%s: failed to get device uuid.\n", __func__); |
| 655 | } | 671 | } |
| 656 | log_debug_msg("%s: device uuid: %s\n", __func__, uuid); | 672 | log_debug_msg("%s: device uuid: %s\n", __func__, client_loc->uuid); |
| 657 | 673 | ||
| 658 | userpref_get_host_id(&host_id); | 674 | userpref_get_host_id(&host_id); |
| 659 | if (LOCKDOWN_E_SUCCESS == ret && !host_id) { | 675 | if (LOCKDOWN_E_SUCCESS == ret && !host_id) { |
| 660 | ret = LOCKDOWN_E_INVALID_CONF; | 676 | ret = LOCKDOWN_E_INVALID_CONF; |
| 661 | } | 677 | } |
| 662 | 678 | ||
| 663 | if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(uuid)) | 679 | if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(client_loc->uuid)) |
| 664 | ret = lockdownd_pair(client_loc, uuid, host_id); | 680 | ret = lockdownd_pair(client_loc, host_id); |
| 665 | |||
| 666 | if (uuid) { | ||
| 667 | free(uuid); | ||
| 668 | uuid = NULL; | ||
| 669 | } | ||
| 670 | 681 | ||
| 671 | if (LOCKDOWN_E_SUCCESS == ret) { | 682 | if (LOCKDOWN_E_SUCCESS == ret) { |
| 672 | ret = lockdownd_start_ssl_session(client_loc, host_id); | 683 | ret = lockdownd_start_ssl_session(client_loc, host_id); |
| @@ -687,12 +698,17 @@ lockdownd_error_t lockdownd_client_new(iphone_device_t device, lockdownd_client_ | |||
| 687 | return ret; | 698 | return ret; |
| 688 | } | 699 | } |
| 689 | 700 | ||
| 690 | /** Generates the appropriate keys and pairs the device. It's part of the | 701 | /** Function used internally by lockdownd_pair() and lockdownd_validate_pair() |
| 691 | * lockdownd handshake. | 702 | * |
| 703 | * @param client The lockdown client to pair with. | ||
| 704 | * @param host_id The HostID to use for pairing. If NULL is passed, then | ||
| 705 | * the HostID of the current machine is used. A new HostID will be | ||
| 706 | * generated automatically when pairing is done for the first time. | ||
| 707 | * @param verb This is either "Pair" or "ValidatePair". | ||
| 692 | * | 708 | * |
| 693 | * @return an error code (LOCKDOWN_E_SUCCESS on success) | 709 | * @return an error code (LOCKDOWN_E_SUCCESS on success) |
| 694 | */ | 710 | */ |
| 695 | lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *host_id) | 711 | static lockdownd_error_t lockdownd_do_pair(lockdownd_client_t client, char *host_id, const char *verb) |
| 696 | { | 712 | { |
| 697 | lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; | 713 | lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 698 | plist_t dict = NULL; | 714 | plist_t dict = NULL; |
| @@ -703,6 +719,8 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho | |||
| 703 | gnutls_datum_t root_cert = { NULL, 0 }; | 719 | gnutls_datum_t root_cert = { NULL, 0 }; |
| 704 | gnutls_datum_t public_key = { NULL, 0 }; | 720 | gnutls_datum_t public_key = { NULL, 0 }; |
| 705 | 721 | ||
| 722 | char *host_id_loc = host_id; | ||
| 723 | |||
| 706 | ret = lockdownd_get_device_public_key(client, &public_key); | 724 | ret = lockdownd_get_device_public_key(client, &public_key); |
| 707 | if (ret != LOCKDOWN_E_SUCCESS) { | 725 | if (ret != LOCKDOWN_E_SUCCESS) { |
| 708 | log_debug_msg("%s: device refused to send public key.\n", __func__); | 726 | log_debug_msg("%s: device refused to send public key.\n", __func__); |
| @@ -716,6 +734,10 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho | |||
| 716 | return ret; | 734 | return ret; |
| 717 | } | 735 | } |
| 718 | 736 | ||
| 737 | if (!host_id) { | ||
| 738 | userpref_get_host_id(&host_id_loc); | ||
| 739 | } | ||
| 740 | |||
| 719 | /* Setup Pair request plist */ | 741 | /* Setup Pair request plist */ |
| 720 | dict = plist_new_dict(); | 742 | dict = plist_new_dict(); |
| 721 | dict_record = plist_new_dict(); | 743 | dict_record = plist_new_dict(); |
| @@ -723,16 +745,20 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho | |||
| 723 | 745 | ||
| 724 | plist_dict_insert_item(dict_record, "DeviceCertificate", plist_new_data((const char*)device_cert.data, device_cert.size)); | 746 | plist_dict_insert_item(dict_record, "DeviceCertificate", plist_new_data((const char*)device_cert.data, device_cert.size)); |
| 725 | plist_dict_insert_item(dict_record, "HostCertificate", plist_new_data((const char*)host_cert.data, host_cert.size)); | 747 | plist_dict_insert_item(dict_record, "HostCertificate", plist_new_data((const char*)host_cert.data, host_cert.size)); |
| 726 | plist_dict_insert_item(dict_record, "HostID", plist_new_string(host_id)); | 748 | plist_dict_insert_item(dict_record, "HostID", plist_new_string(host_id_loc)); |
| 727 | plist_dict_insert_item(dict_record, "RootCertificate", plist_new_data((const char*)root_cert.data, root_cert.size)); | 749 | plist_dict_insert_item(dict_record, "RootCertificate", plist_new_data((const char*)root_cert.data, root_cert.size)); |
| 728 | 750 | ||
| 729 | plist_dict_insert_item(dict, "Request", plist_new_string("Pair")); | 751 | plist_dict_insert_item(dict, "Request", plist_new_string(verb)); |
| 730 | 752 | ||
| 731 | /* send to iPhone */ | 753 | /* send to iPhone */ |
| 732 | ret = lockdownd_send(client, dict); | 754 | ret = lockdownd_send(client, dict); |
| 733 | plist_free(dict); | 755 | plist_free(dict); |
| 734 | dict = NULL; | 756 | dict = NULL; |
| 735 | 757 | ||
| 758 | if (!host_id) { | ||
| 759 | free(host_id_loc); | ||
| 760 | } | ||
| 761 | |||
| 736 | if (ret != LOCKDOWN_E_SUCCESS) | 762 | if (ret != LOCKDOWN_E_SUCCESS) |
| 737 | return ret; | 763 | return ret; |
| 738 | 764 | ||
| @@ -742,24 +768,56 @@ lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *uuid, char *ho | |||
| 742 | if (ret != LOCKDOWN_E_SUCCESS) | 768 | if (ret != LOCKDOWN_E_SUCCESS) |
| 743 | return ret; | 769 | return ret; |
| 744 | 770 | ||
| 745 | if (lockdown_check_result(dict, "Pair") == RESULT_SUCCESS) { | 771 | if (lockdown_check_result(dict, verb) != RESULT_SUCCESS) { |
| 746 | ret = LOCKDOWN_E_SUCCESS; | 772 | ret = LOCKDOWN_E_PAIRING_FAILED; |
| 747 | } | 773 | } |
| 748 | plist_free(dict); | 774 | plist_free(dict); |
| 749 | dict = NULL; | 775 | dict = NULL; |
| 750 | 776 | ||
| 751 | /* store public key in config if pairing succeeded */ | 777 | /* store public key in config if pairing succeeded */ |
| 752 | if (ret == LOCKDOWN_E_SUCCESS) { | 778 | if (ret == LOCKDOWN_E_SUCCESS) { |
| 753 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair success\n", __func__); | 779 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: %s success\n", __func__, verb); |
| 754 | userpref_set_device_public_key(uuid, public_key); | 780 | userpref_set_device_public_key(client->uuid, public_key); |
| 755 | } else { | 781 | } else { |
| 756 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: pair failure\n", __func__); | 782 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: %s failure\n", __func__, verb); |
| 757 | ret = LOCKDOWN_E_PAIRING_FAILED; | ||
| 758 | } | 783 | } |
| 759 | free(public_key.data); | 784 | free(public_key.data); |
| 760 | return ret; | 785 | return ret; |
| 761 | } | 786 | } |
| 762 | 787 | ||
| 788 | /** | ||
| 789 | * Pairs the device with the given HostID. | ||
| 790 | * It's part of the lockdownd handshake. | ||
| 791 | * | ||
| 792 | * @param client The lockdown client to pair with. | ||
| 793 | * @param host_id The HostID to use for pairing. If NULL is passed, then | ||
| 794 | * the HostID of the current machine is used. A new HostID will be | ||
| 795 | * generated automatically when pairing is done for the first time. | ||
| 796 | * | ||
| 797 | * @return an error code (LOCKDOWN_E_SUCCESS on success) | ||
| 798 | */ | ||
| 799 | lockdownd_error_t lockdownd_pair(lockdownd_client_t client, char *host_id) | ||
| 800 | { | ||
| 801 | return lockdownd_do_pair(client, host_id, "Pair"); | ||
| 802 | } | ||
| 803 | |||
| 804 | /** | ||
| 805 | * Pairs the device with the given HostID. The difference to lockdownd_pair() | ||
| 806 | * is that the specified host will become trusted host of the device. | ||
| 807 | * It's part of the lockdownd handshake. | ||
| 808 | * | ||
| 809 | * @param client The lockdown client to pair with. | ||
| 810 | * @param host_id The HostID to use for pairing. If NULL is passed, then | ||
| 811 | * the HostID of the current machine is used. A new HostID will be | ||
| 812 | * generated automatically when pairing is done for the first time. | ||
| 813 | * | ||
| 814 | * @return an error code (LOCKDOWN_E_SUCCESS on success) | ||
| 815 | */ | ||
| 816 | lockdownd_error_t lockdownd_validate_pair(lockdownd_client_t client, char *host_id) | ||
| 817 | { | ||
| 818 | return lockdownd_do_pair(client, host_id, "ValidatePair"); | ||
| 819 | } | ||
| 820 | |||
| 763 | /** | 821 | /** |
| 764 | * Tells the device to immediately enter recovery mode. | 822 | * Tells the device to immediately enter recovery mode. |
| 765 | * | 823 | * |
| @@ -985,7 +1043,10 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c | |||
| 985 | uint32_t return_me = 0; | 1043 | uint32_t return_me = 0; |
| 986 | 1044 | ||
| 987 | lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; | 1045 | lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 988 | client->session_id[0] = '\0'; | 1046 | if (client->session_id) { |
| 1047 | free(client->session_id); | ||
| 1048 | client->session_id = NULL; | ||
| 1049 | } | ||
| 989 | 1050 | ||
| 990 | /* Setup DevicePublicKey request plist */ | 1051 | /* Setup DevicePublicKey request plist */ |
| 991 | dict = plist_new_dict(); | 1052 | dict = plist_new_dict(); |
| @@ -1012,26 +1073,22 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c | |||
| 1012 | 1073 | ||
| 1013 | if (!strcmp(error, "InvalidHostID")) { | 1074 | if (!strcmp(error, "InvalidHostID")) { |
| 1014 | /* hostid is unknown. Pair and try again */ | 1075 | /* hostid is unknown. Pair and try again */ |
| 1015 | char *uuid = NULL; | ||
| 1016 | char *host_id = NULL; | 1076 | char *host_id = NULL; |
| 1017 | userpref_get_host_id(&host_id); | 1077 | userpref_get_host_id(&host_id); |
| 1018 | 1078 | ||
| 1019 | if (LOCKDOWN_E_SUCCESS == lockdownd_get_device_uuid(client, &uuid) ) { | 1079 | if (LOCKDOWN_E_SUCCESS == lockdownd_pair(client, host_id) ) { |
| 1020 | if (LOCKDOWN_E_SUCCESS == lockdownd_pair(client, uuid, host_id) ) { | 1080 | /* start session again */ |
| 1021 | /* start session again */ | 1081 | plist_free(dict); |
| 1022 | plist_free(dict); | 1082 | dict = plist_new_dict(); |
| 1023 | dict = plist_new_dict(); | 1083 | plist_dict_insert_item(dict,"HostID", plist_new_string(HostID)); |
| 1024 | plist_dict_insert_item(dict,"HostID", plist_new_string(HostID)); | 1084 | plist_dict_insert_item(dict,"Request", plist_new_string("StartSession")); |
| 1025 | plist_dict_insert_item(dict,"Request", plist_new_string("StartSession")); | ||
| 1026 | 1085 | ||
| 1027 | ret = lockdownd_send(client, dict); | 1086 | ret = lockdownd_send(client, dict); |
| 1028 | plist_free(dict); | 1087 | plist_free(dict); |
| 1029 | dict = NULL; | 1088 | dict = NULL; |
| 1030 | 1089 | ||
| 1031 | ret = lockdownd_recv(client, &dict); | 1090 | ret = lockdownd_recv(client, &dict); |
| 1032 | } | ||
| 1033 | } | 1091 | } |
| 1034 | free(uuid); | ||
| 1035 | free(host_id); | 1092 | free(host_id); |
| 1036 | } | 1093 | } |
| 1037 | free(error); | 1094 | free(error); |
| @@ -1100,27 +1157,16 @@ lockdownd_error_t lockdownd_start_ssl_session(lockdownd_client_t client, const c | |||
| 1100 | ret = LOCKDOWN_E_SUCCESS; | 1157 | ret = LOCKDOWN_E_SUCCESS; |
| 1101 | } | 1158 | } |
| 1102 | } | 1159 | } |
| 1103 | /* store session id */ | 1160 | /* store session id, we need it for StopSession */ |
| 1104 | plist_t session_node = plist_dict_get_item(dict, "SessionID"); | 1161 | plist_t session_node = plist_dict_get_item(dict, "SessionID"); |
| 1105 | if (session_node) { | 1162 | if (session_node && (plist_get_node_type(session_node) == PLIST_STRING)) { |
| 1106 | 1163 | plist_get_string_val(session_node, &client->session_id); | |
| 1107 | plist_type session_node_type = plist_get_node_type(session_node); | 1164 | } |
| 1108 | 1165 | if (client->session_id) { | |
| 1109 | if (session_node_type == PLIST_STRING) { | 1166 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: SessionID: %s\n", __func__, client->session_id); |
| 1110 | 1167 | } else { | |
| 1111 | char *session_id = NULL; | ||
| 1112 | plist_get_string_val(session_node, &session_id); | ||
| 1113 | |||
| 1114 | if (session_node_type == PLIST_STRING && session_id) { | ||
| 1115 | /* we need to store the session ID for StopSession */ | ||
| 1116 | strcpy(client->session_id, session_id); | ||
| 1117 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: SessionID: %s\n", __func__, client->session_id); | ||
| 1118 | } | ||
| 1119 | if (session_id) | ||
| 1120 | free(session_id); | ||
| 1121 | } | ||
| 1122 | } else | ||
| 1123 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Failed to get SessionID!\n", __func__); | 1168 | log_dbg_msg(DBGMASK_LOCKDOWND, "%s: Failed to get SessionID!\n", __func__); |
| 1169 | } | ||
| 1124 | plist_free(dict); | 1170 | plist_free(dict); |
| 1125 | dict = NULL; | 1171 | dict = NULL; |
| 1126 | 1172 | ||
diff --git a/src/lockdown.h b/src/lockdown.h index 9312867..931623a 100644 --- a/src/lockdown.h +++ b/src/lockdown.h | |||
| @@ -32,7 +32,8 @@ struct lockdownd_client_int { | |||
| 32 | gnutls_session_t ssl_session; | 32 | gnutls_session_t ssl_session; |
| 33 | gnutls_certificate_credentials_t ssl_certificate; | 33 | gnutls_certificate_credentials_t ssl_certificate; |
| 34 | int in_SSL; | 34 | int in_SSL; |
| 35 | char session_id[40]; | 35 | char *session_id; |
| 36 | char *uuid; | ||
| 36 | }; | 37 | }; |
| 37 | 38 | ||
| 38 | lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); | 39 | lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, gnutls_datum_t * public_key); |
