int restore_check_mode(const char* uuid) { char* type = NULL; uint64_t version = 0; idevice_t device = NULL; restored_client_t restore = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; restored_error_t restore_error = RESTORE_E_SUCCESS; device_error = idevice_new(&device, uuid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } restore_error = restored_client_new(device, &restore, "idevicerestore"); if (restore_error != RESTORE_E_SUCCESS) { idevice_free(device); return -1; } restore_error = restored_query_type(restore, &type, &version); if (restore_error != RESTORE_E_SUCCESS) { restored_client_free(restore); idevice_free(device); return -1; } restored_client_free(restore); idevice_free(device); restore = NULL; device = NULL; return 0; }
static int start_logging(void) { idevice_error_t ret = idevice_new(&device, udid); if (ret != IDEVICE_E_SUCCESS) { fprintf(stderr, "Device with udid %s not found!?\n", udid); return -1; } /* start and connect to syslog_relay service */ syslog_relay_error_t serr = SYSLOG_RELAY_E_UNKNOWN_ERROR; serr = syslog_relay_client_start_service(device, &syslog, "idevicesyslog"); if (serr != SYSLOG_RELAY_E_SUCCESS) { fprintf(stderr, "ERROR: Could not start service com.apple.syslog_relay.\n"); idevice_free(device); device = NULL; return -1; } /* start capturing syslog */ serr = syslog_relay_start_capture(syslog, syslog_callback, NULL); if (serr != SYSLOG_RELAY_E_SUCCESS) { fprintf(stderr, "ERROR: Unable tot start capturing syslog.\n"); syslog_relay_client_free(syslog); syslog = NULL; idevice_free(device); device = NULL; return -1; } fprintf(stdout, "[connected]\n"); fflush(stdout); return 0; }
int normal_check_mode(const char* uuid) { char* type = NULL; idevice_t device = NULL; lockdownd_client_t lockdown = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = IDEVICE_E_SUCCESS; device_error = idevice_new(&device, uuid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { idevice_free(device); return -1; } lockdown_error = lockdownd_query_type(lockdown, &type); if (lockdown_error != LOCKDOWN_E_SUCCESS) { lockdownd_client_free(lockdown); idevice_free(device); return -1; } lockdownd_client_free(lockdown); idevice_free(device); lockdown = NULL; device = NULL; return 0; }
/* * Class: com_att_aro_libimobiledevice_Screencapture * Method: startService * Signature: ()V */ JNIEXPORT jstring JNICALL Java_com_att_aro_libimobiledevice_ScreencaptureImpl_startService (JNIEnv * env, jobject obj) { const char * str;// = "SUCCESS"; if (IDEVICE_E_SUCCESS != idevice_new(&device, udid)) { str = "No device found, is it plugged in?\n"; return outPut(env, str); } if(!device) { str = "No device mounted"; return outPut(env, str); } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, NULL)) { idevice_free(device); str = "Failed to acquire lock service."; return outPut(env, str); } lockdownd_start_service(lckd, "com.apple.mobile.screenshotr", &service); lockdownd_client_free(lckd); if (service && service->port > 0) { if (screenshotr_client_new(device, service, &shotr) != SCREENSHOTR_E_SUCCESS) { str = "Could not connect to screenshotr service!"; } else { str = "SUCCESS"; } } else { str = "Could not start screenshotr service! Try running Instruments tool from XCode on this device and see if it work, then try this again."; } return outPut(env, str); }
int restore_check_device(const char* uuid) { int i = 0; char* type = NULL; char* model = NULL; plist_t node = NULL; uint64_t version = 0; idevice_t device = NULL; restored_client_t restore = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; restored_error_t restore_error = RESTORE_E_SUCCESS; device_error = idevice_new(&device, uuid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } restore_error = restored_client_new(device, &restore, "idevicerestore"); if (restore_error != RESTORE_E_SUCCESS) { idevice_free(device); return -1; } restore_error = restored_query_type(restore, &type, &version); if (restore_error != RESTORE_E_SUCCESS) { restored_client_free(restore); idevice_free(device); return -1; } restore_error = restored_get_value(restore, "HardwareModel", &node); if (restore_error != RESTORE_E_SUCCESS) { error("ERROR: Unable to get HardwareModel from restored\n"); restored_client_free(restore); idevice_free(device); return -1; } restored_client_free(restore); idevice_free(device); restore = NULL; device = NULL; if (!node || plist_get_node_type(node) != PLIST_STRING) { error("ERROR: Unable to get HardwareModel information\n"); if (node) plist_free(node); return -1; } plist_get_string_val(node, &model); for (i = 0; idevicerestore_devices[i].model != NULL; i++) { if (!strcasecmp(model, idevicerestore_devices[i].model)) { break; } } return idevicerestore_devices[i].device_id; }
int normal_enter_recovery(struct idevicerestore_client_t* client) { idevice_t device = NULL; irecv_client_t recovery = NULL; lockdownd_client_t lockdown = NULL; irecv_error_t recovery_error = IRECV_E_SUCCESS; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; device_error = idevice_new(&device, client->uuid); if (device_error != IDEVICE_E_SUCCESS) { error("ERROR: Unable to find device\n"); return -1; } lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to connect to lockdownd service\n"); idevice_free(device); return -1; } lockdown_error = lockdownd_enter_recovery(lockdown); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to place device in recovery mode\n"); lockdownd_client_free(lockdown); idevice_free(device); return -1; } lockdownd_client_free(lockdown); idevice_free(device); lockdown = NULL; device = NULL; if (recovery_open_with_timeout(client) < 0) { error("ERROR: Unable to enter recovery mode\n"); return -1; } recovery_error = irecv_send_command(recovery, "setenv auto-boot true"); if (recovery_error != IRECV_E_SUCCESS) { error("ERROR: Unable to reset auto-boot variable\n"); irecv_close(recovery); return -1; } recovery_error = irecv_send_command(recovery, "saveenv"); if (recovery_error != IRECV_E_SUCCESS) { error("ERROR: Unable to save auto-boot variable\n"); irecv_close(recovery); return -1; } //client->mode = &idevicerestore_modes[MODE_RECOVERY]; irecv_close(recovery); recovery = NULL; return 0; }
int main(int argc, char *argv[]) { lockdownd_client_t client = NULL; idevice_t phone = NULL; GError *err; uint16_t port = 0; afc_client_t afc = NULL; if (argc > 1 && !strcasecmp(argv[1], "--debug")) { idevice_set_debug_level(1); } else { idevice_set_debug_level(0); } if (IDEVICE_E_SUCCESS != idevice_new(&phone, NULL)) { printf("No device found, is it plugged in?\n"); return 1; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "afccheck")) { idevice_free(phone); return 1; } if (LOCKDOWN_E_SUCCESS == lockdownd_start_service(client, "com.apple.afc", &port) && !port) { lockdownd_client_free(client); idevice_free(phone); fprintf(stderr, "Something went wrong when starting AFC."); return 1; } afc_client_new(phone, port, &afc); //makes sure thread environment is available if (!g_thread_supported()) g_thread_init(NULL); GThread *threads[NB_THREADS]; param data[NB_THREADS]; int i = 0; for (i = 0; i < NB_THREADS; i++) { data[i].afc = afc; data[i].id = i + 1; threads[i] = g_thread_create((GThreadFunc) check_afc, data + i, TRUE, &err); } for (i = 0; i < NB_THREADS; i++) { g_thread_join(threads[i]); } lockdownd_client_free(client); idevice_free(phone); return 0; }
int restore_open_with_timeout(const char* uuid, idevice_t* device, restored_client_t* restore) { int i = 0; int attempts = 10; idevice_t context = NULL; restored_client_t client = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; restored_error_t restore_error = RESTORE_E_SUCCESS; *device = NULL; *restore = NULL; device_error = idevice_event_subscribe(&restore_device_callback, NULL); if (device_error != IDEVICE_E_SUCCESS) { error("ERROR: Unable to subscribe to device events\n"); return -1; } for (i = 1; i <= attempts; i++) { if (restore_device_connected == 1) { break; } if (i == attempts) { error("ERROR: Unable to connect to device in restore mode\n"); } sleep(2); } device_error = idevice_new(&context, uuid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } restore_error = restored_client_new(context, &client, "idevicerestore"); if (restore_error != RESTORE_E_SUCCESS) { idevice_event_unsubscribe(); idevice_free(context); return -1; } restore_error = restored_query_type(client, NULL, NULL); if (restore_error != RESTORE_E_SUCCESS) { restored_client_free(client); idevice_event_unsubscribe(); idevice_free(context); return -1; } *device = context; *restore = client; return 0; }
int main(int argc, char *argv[]) { lockdownd_client_t client = NULL; idevice_t phone = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; int i; char uuid[41]; uuid[0] = 0; /* parse cmdline args */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { idevice_set_debug_level(1); continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); return 0; } } i--; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); return 0; } strcpy(uuid, argv[i]); ret = idevice_new(&phone, uuid); if (ret != IDEVICE_E_SUCCESS) { printf("No device found with uuid %s, is it plugged in?\n", uuid); return -1; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client, "ideviceenterrecovery")) { idevice_free(phone); return -1; } /* run query and output information */ printf("Telling device with uuid %s to enter recovery mode.}\n", uuid); if(lockdownd_enter_recovery(client) != LOCKDOWN_E_SUCCESS) { printf("Failed to enter recovery mode.\n"); } printf("Device is successfully switching to recovery mode.\n"); lockdownd_client_free(client); idevice_free(phone); return 0; }
int deviceConnect() { idevice_error_t ideverr = 0; ideverr = idevice_new(&gDevice, NULL); if (ideverr != IDEVICE_E_SUCCESS) { return -1; } // grab UDID to show which device we're connected to. ideverr = idevice_get_udid(gDevice, &udid); if (ideverr != IDEVICE_E_SUCCESS) { printf("%s Unable to communicate with device.%s\n", KRED, KNRM); } printf(" UDID: %s!\n", udid); return 0; }
int normal_check_device(const char* uuid) { int i = 0; idevice_t device = NULL; char* product_type = NULL; plist_t product_type_node = NULL; lockdownd_client_t lockdown = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = IDEVICE_E_SUCCESS; device_error = idevice_new(&device, uuid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } lockdown_error = lockdownd_client_new_with_handshake(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { idevice_free(device); return -1; } lockdown_error = lockdownd_get_value(lockdown, NULL, "ProductType", &product_type_node); if (lockdown_error != LOCKDOWN_E_SUCCESS) { lockdownd_client_free(lockdown); idevice_free(device); return -1; } lockdownd_client_free(lockdown); idevice_free(device); lockdown = NULL; device = NULL; if (!product_type_node || plist_get_node_type(product_type_node) != PLIST_STRING) { if (product_type_node) plist_free(product_type_node); return -1; } plist_get_string_val(product_type_node, &product_type); plist_free(product_type_node); for (i = 0; idevicerestore_devices[i].product != NULL; i++) { if (!strcmp(product_type, idevicerestore_devices[i].product)) { break; } } return idevicerestore_devices[i].index; }
int enter_recovery() { lockdownd_client_t client = NULL; idevice_t phone = NULL; idevice_t ret = IDEVICE_E_UNKNOWN_ERROR; ret = idevice_new(&phone, NULL); if (ret == IDEVICE_E_SUCCESS) { if (lockdownd_client_new_with_handshake(phone, &client, "ideviceenterrecovery") != LOCKDOWN_E_SUCCESS) { idevice_free(phone); } } if (lockdownd_enter_recovery(client) != LOCKDOWN_E_SUCCESS) { printf("Failed to enter recovery"); return -1; //Failed to enter recovery } printf("entered recovery"); return 0; //Success! }
int normal_get_nonce(struct idevicerestore_client_t* client, unsigned char** nonce, int* nonce_size) { idevice_t device = NULL; plist_t nonce_node = NULL; lockdownd_client_t lockdown = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = IDEVICE_E_SUCCESS; device_error = idevice_new(&device, client->udid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to connect to lockdownd\n"); idevice_free(device); return -1; } lockdown_error = lockdownd_get_value(lockdown, NULL, "ApNonce", &nonce_node); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to get ApNonce from lockdownd\n"); lockdownd_client_free(lockdown); idevice_free(device); return -1; } if (!nonce_node || plist_get_node_type(nonce_node) != PLIST_DATA) { error("ERROR: Unable to get nonce\n"); lockdownd_client_free(lockdown); idevice_free(device); return -1; } uint64_t n_size = 0; plist_get_data_val(nonce_node, (char**)nonce, &n_size); *nonce_size = (int)n_size; plist_free(nonce_node); lockdownd_client_free(lockdown); idevice_free(device); lockdown = NULL; device = NULL; return 0; }
SWIGEXPORT jshort JNICALL Java_org_robovm_libimobiledevice_binding_libimobiledeviceJNI_idevice_1new(JNIEnv *jenv, jclass jcls, jlong jarg1, jstring jarg2) { jshort jresult = 0 ; idevice_t *arg1 = (idevice_t *) 0 ; char *arg2 = (char *) 0 ; idevice_error_t result; (void)jenv; (void)jcls; arg1 = *(idevice_t **)&jarg1; arg2 = 0; if (jarg2) { arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); if (!arg2) return 0; } result = (idevice_error_t)idevice_new(arg1,(char const *)arg2); jresult = (jshort)result; if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, (const char *)arg2); return jresult; }
int normal_enter_recovery(struct idevicerestore_client_t* client) { idevice_t device = NULL; irecv_client_t recovery = NULL; lockdownd_client_t lockdown = NULL; irecv_error_t recovery_error = IRECV_E_SUCCESS; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = LOCKDOWN_E_SUCCESS; device_error = idevice_new(&device, client->uuid); if (device_error != IDEVICE_E_SUCCESS) { error("ERROR: Unable to find device\n"); return -1; } lockdown_error = lockdownd_client_new(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to connect to lockdownd service\n"); idevice_free(device); return -1; } lockdown_error = lockdownd_enter_recovery(lockdown); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to place device in recovery mode\n"); lockdownd_client_free(lockdown); idevice_free(device); return -1; } lockdownd_client_free(lockdown); idevice_free(device); lockdown = NULL; device = NULL; if (recovery_client_new(client) < 0) { error("ERROR: Unable to enter recovery mode\n"); return -1; } client->mode = &idevicerestore_modes[MODE_RECOVERY]; recovery = NULL; return 0; }
int normal_get_ecid(struct idevicerestore_client_t* client, uint64_t* ecid) { idevice_t device = NULL; plist_t unique_chip_node = NULL; lockdownd_client_t lockdown = NULL; idevice_error_t device_error = IDEVICE_E_SUCCESS; lockdownd_error_t lockdown_error = IDEVICE_E_SUCCESS; device_error = idevice_new(&device, client->uuid); if (device_error != IDEVICE_E_SUCCESS) { return -1; } lockdown_error = lockdownd_client_new_with_handshake(device, &lockdown, "idevicerestore"); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to connect to lockdownd\n"); idevice_free(device); return -1; } lockdown_error = lockdownd_get_value(lockdown, NULL, "UniqueChipID", &unique_chip_node); if (lockdown_error != LOCKDOWN_E_SUCCESS) { error("ERROR: Unable to get UniqueChipID from lockdownd\n"); lockdownd_client_free(lockdown); idevice_free(device); return -1; } if (!unique_chip_node || plist_get_node_type(unique_chip_node) != PLIST_UINT) { error("ERROR: Unable to get ECID\n"); lockdownd_client_free(lockdown); idevice_free(device); return -1; } plist_get_uint_val(unique_chip_node, ecid); plist_free(unique_chip_node); lockdownd_client_free(lockdown); idevice_free(device); lockdown = NULL; device = NULL; return 0; }
Device::Device(const char *udid) { new (this)Device(); this->mUdid = udid; if (idevice_new(&this->mDevice, this->mUdid.c_str()) != IDEVICE_E_SUCCESS) { this->mStatus = StatusError; return; } if (lockdownd_client_new_with_handshake(this->mDevice, &this->mLockDown, NULL) != LOCKDOWN_E_SUCCESS) { this->mStatus = StatusError; return; } if (instproxy_client_start_service(this->mDevice, &this->mInstProxy, NULL) != INSTPROXY_E_SUCCESS) { this->mStatus = StatusError; return; } if (this->getName() != DeviceStatus::StatusOK) { this->mStatus = StatusError; return; } if (this->getApps() != DeviceStatus::StatusOK) { this->mStatus = StatusError; return; } }
int main(int argc, char **argv) { idevice_t device = NULL; lockdownd_client_t lockdown_client = NULL; diagnostics_relay_client_t diagnostics_client = NULL; lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; uint16_t port = 0; int result = -1; int i; const char *udid = NULL; int cmd = CMD_NONE; char* cmd_arg = NULL; plist_t node = NULL; plist_t keys = NULL; /* parse cmdline args */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { idevice_set_debug_level(1); continue; } else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { i++; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); result = 0; goto cleanup; } udid = argv[i]; continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); result = 0; goto cleanup; } else if (!strcmp(argv[i], "sleep")) { cmd = CMD_SLEEP; } else if (!strcmp(argv[i], "restart")) { cmd = CMD_RESTART; } else if (!strcmp(argv[i], "shutdown")) { cmd = CMD_SHUTDOWN; } else if (!strcmp(argv[i], "diagnostics")) { cmd = CMD_DIAGNOSTICS; /* read type */ i++; if (!argv[i] || ((strcmp(argv[i], "All") != 0) && (strcmp(argv[i], "WiFi") != 0) && (strcmp(argv[i], "GasGauge") != 0) && (strcmp(argv[i], "NAND") != 0))) { if (argv[i] == NULL) { cmd_arg = strdup("All"); continue; } if (!strncmp(argv[i], "-", 1)) { cmd_arg = strdup("All"); i--; continue; } printf("Unknown TYPE %s\n", argv[i]); print_usage(argc, argv); goto cleanup; } cmd_arg = strdup(argv[i]); continue; } else if (!strcmp(argv[i], "mobilegestalt")) { cmd = CMD_MOBILEGESTALT; /* read keys */ i++; if (!argv[i] || argv[i] == NULL || (!strncmp(argv[i], "-", 1))) { printf("Please supply the key to query.\n"); print_usage(argc, argv); goto cleanup; } keys = plist_new_array(); while(1) { if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) { plist_array_append_item(keys, plist_new_string(argv[i])); i++; } else { i--; break; } } continue; } else if (!strcmp(argv[i], "ioreg")) { cmd = CMD_IOREGISTRY; /* read plane */ i++; if (argv[i]) { cmd_arg = strdup(argv[i]); } continue; } else { print_usage(argc, argv); return 0; } } /* verify options */ if (cmd == CMD_NONE) { print_usage(argc, argv); goto cleanup; } if (IDEVICE_E_SUCCESS != idevice_new(&device, udid)) { if (udid) { printf("No device found with udid %s, is it plugged in?\n", udid); } else { printf("No device found, is it plugged in?\n"); } goto cleanup; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lockdown_client, NULL)) { idevice_free(device); printf("Unable to connect to lockdownd.\n"); goto cleanup; } /* attempt to use newer diagnostics service available on iOS 5 and later */ ret = lockdownd_start_service(lockdown_client, "com.apple.mobile.diagnostics_relay", &port); if (ret != LOCKDOWN_E_SUCCESS) { /* attempt to use older diagnostics service */ ret = lockdownd_start_service(lockdown_client, "com.apple.iosdiagnostics.relay", &port); } lockdownd_client_free(lockdown_client); if ((ret == LOCKDOWN_E_SUCCESS) && (port > 0)) { if (diagnostics_relay_client_new(device, port, &diagnostics_client) != DIAGNOSTICS_RELAY_E_SUCCESS) { printf("Could not connect to diagnostics_relay!\n"); result = -1; } else { switch (cmd) { case CMD_SLEEP: if (diagnostics_relay_sleep(diagnostics_client) == DIAGNOSTICS_RELAY_E_SUCCESS) { printf("Putting device into deep sleep mode.\n"); result = EXIT_SUCCESS; } else { printf("Failed to put device into deep sleep mode.\n"); } break; case CMD_RESTART: if (diagnostics_relay_restart(diagnostics_client, 0) == DIAGNOSTICS_RELAY_E_SUCCESS) { printf("Restarting device.\n"); result = EXIT_SUCCESS; } else { printf("Failed to restart device.\n"); } break; case CMD_SHUTDOWN: if (diagnostics_relay_shutdown(diagnostics_client, 0) == DIAGNOSTICS_RELAY_E_SUCCESS) { printf("Shutting down device.\n"); result = EXIT_SUCCESS; } else { printf("Failed to shutdown device.\n"); } break; case CMD_MOBILEGESTALT: if (diagnostics_relay_query_mobilegestalt(diagnostics_client, keys, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { if (node) { print_xml(node); result = EXIT_SUCCESS; } } else { printf("Unable to query mobilegestalt keys.\n"); } break; case CMD_IOREGISTRY: if (diagnostics_relay_query_ioregistry_plane(diagnostics_client, cmd_arg == NULL ? "": cmd_arg, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { if (node) { print_xml(node); result = EXIT_SUCCESS; } } else { printf("Unable to retrieve IORegistry from device.\n"); } break; case CMD_DIAGNOSTICS: default: if (diagnostics_relay_request_diagnostics(diagnostics_client, cmd_arg, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { if (node) { print_xml(node); result = EXIT_SUCCESS; } } else { printf("Unable to retrieve diagnostics from device.\n"); } break; } diagnostics_relay_goodbye(diagnostics_client); diagnostics_relay_client_free(diagnostics_client); } } else { printf("Could not start diagnostics service!\n"); } idevice_free(device); cleanup: if (node) { plist_free(node); } if (keys) { plist_free(keys); } if (cmd_arg) { free(cmd_arg); } return result; }
int main(int argc, char *argv[]) { lockdownd_client_t client = NULL; idevice_t phone = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; int i; int simple = 0; int format = FORMAT_KEY_VALUE; char uuid[41]; char *domain = NULL; char *key = NULL; char *xml_doc = NULL; uint32_t xml_length; plist_t node = NULL; plist_type node_type; uuid[0] = 0; /* parse cmdline args */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { idevice_set_debug_level(1); continue; } else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { i++; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); return 0; } strcpy(uuid, argv[i]); continue; } else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) { i++; if (!argv[i] || (strlen(argv[i]) < 4)) { print_usage(argc, argv); return 0; } if (!is_domain_known(argv[i])) { fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]); } domain = strdup(argv[i]); continue; } else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) { i++; if (!argv[i] || (strlen(argv[i]) <= 1)) { print_usage(argc, argv); return 0; } key = strdup(argv[i]); continue; } else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) { format = FORMAT_XML; continue; } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--simple")) { simple = 1; continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); return 0; } else { print_usage(argc, argv); return 0; } } if (uuid[0] != 0) { ret = idevice_new(&phone, uuid); if (ret != IDEVICE_E_SUCCESS) { printf("No device found with uuid %s, is it plugged in?\n", uuid); return -1; } } else { ret = idevice_new(&phone, NULL); if (ret != IDEVICE_E_SUCCESS) { printf("No device found, is it plugged in?\n"); return -1; } } if (LOCKDOWN_E_SUCCESS != (simple ? lockdownd_client_new(phone, &client, "ideviceinfo"): lockdownd_client_new_with_handshake(phone, &client, "ideviceinfo"))) { idevice_free(phone); return -1; } /* run query and output information */ if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) { if (node) { switch (format) { case FORMAT_XML: plist_to_xml(node, &xml_doc, &xml_length); printf("%s", xml_doc); free(xml_doc); break; case FORMAT_KEY_VALUE: node_type = plist_get_node_type(node); if (node_type == PLIST_DICT) { plist_dict_to_string(node); } else if (node_type == PLIST_ARRAY) { plist_array_to_string(node); break; } default: if (key != NULL) plist_node_to_string(node); break; } plist_free(node); node = NULL; } } if (domain != NULL) free(domain); lockdownd_client_free(client); idevice_free(phone); return 0; }
static int normal_idevice_new(struct idevicerestore_client_t* client, idevice_t* device) { int num_devices = 0; char **devices = NULL; idevice_get_device_list(&devices, &num_devices); if (num_devices == 0) { return -1; } *device = NULL; idevice_t dev = NULL; idevice_error_t device_error; lockdownd_client_t lockdown = NULL; int j; for (j = 0; j < num_devices; j++) { if (lockdown != NULL) { lockdownd_client_free(lockdown); lockdown = NULL; } if (dev != NULL) { idevice_free(dev); dev = NULL; } device_error = idevice_new(&dev, devices[j]); if (device_error != IDEVICE_E_SUCCESS) { error("ERROR: %s: can't open device with UUID %s", __func__, devices[j]); continue; } if (lockdownd_client_new(dev, &lockdown, "idevicerestore") != LOCKDOWN_E_SUCCESS) { error("ERROR: %s: can't connect to lockdownd on device with UUID %s", __func__, devices[j]); continue; } char* type = NULL; if (lockdownd_query_type(lockdown, &type) != LOCKDOWN_E_SUCCESS) { continue; } if (strcmp(type, "com.apple.mobile.lockdown") != 0) { free(type); continue; } free(type); if (client->ecid != 0) { plist_t node = NULL; if ((lockdownd_get_value(lockdown, NULL, "UniqueChipID", &node) != LOCKDOWN_E_SUCCESS) || !node || (plist_get_node_type(node) != PLIST_UINT)){ if (node) { plist_free(node); } continue; } lockdownd_client_free(lockdown); lockdown = NULL; uint64_t this_ecid = 0; plist_get_uint_val(node, &this_ecid); plist_free(node); if (this_ecid != client->ecid) { continue; } } if (lockdown) { lockdownd_client_free(lockdown); lockdown = NULL; } client->uuid = strdup(devices[j]); *device = dev; break; } idevice_device_list_free(devices); return 0; }
G_GNUC_INTERNAL gboolean iphone_write_sysinfo_extended (const char *uuid, const char *xml) { lockdownd_client_t client = NULL; idevice_t device = NULL; afc_client_t afc = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; afc_error_t afc_ret; uint16_t afcport = 0; uint64_t handle; uint32_t bytes_written; const char device_dir[] = "/iTunes_Control/Device"; const char sysinfoextended_path[] = "/iTunes_Control/Device/SysInfoExtended"; ret = idevice_new(&device, uuid); if (IDEVICE_E_SUCCESS != ret) { printf("No device found with uuid %s, is it plugged in?\n", uuid); return FALSE; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &client, "libgpod")) { idevice_free(device); return FALSE; } if (LOCKDOWN_E_SUCCESS != lockdownd_start_service(client, "com.apple.afc", &afcport)) { lockdownd_client_free(client); idevice_free(device); return FALSE; } g_assert (afcport != 0); if (AFC_E_SUCCESS != afc_client_new(device, afcport, &afc)) { lockdownd_client_free(client); idevice_free(device); return FALSE; } afc_ret = afc_make_directory(afc, device_dir); if ((AFC_E_SUCCESS != afc_ret) && (AFC_E_OBJECT_EXISTS != afc_ret)) { afc_client_free(afc); lockdownd_client_free(client); idevice_free(device); return FALSE; } if (AFC_E_SUCCESS != afc_file_open(afc, sysinfoextended_path, AFC_FOPEN_WRONLY, &handle)) { afc_client_free(afc); lockdownd_client_free(client); idevice_free(device); return FALSE; } if (AFC_E_SUCCESS != afc_file_write(afc, handle, xml, strlen(xml), &bytes_written)) { afc_file_close(afc, handle); afc_client_free(afc); lockdownd_client_free(client); idevice_free(device); return FALSE; } afc_file_close(afc, handle); afc_client_free(afc); lockdownd_client_free(client); idevice_free(device); return TRUE; }
int main(int argc, char *argv[]) { lockdownd_client_t lockdown = NULL; idevice_t device = NULL; idevice_connection_t connection = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; thread_t th; const char* udid = NULL; lockdownd_service_descriptor_t service = NULL; uint16_t local_port = 0; int result = EXIT_SUCCESS; int i; /* bind signals */ signal(SIGINT, clean_exit); signal(SIGTERM, clean_exit); #ifndef WIN32 signal(SIGQUIT, clean_exit); signal(SIGPIPE, SIG_IGN); #endif /* parse cmdline arguments */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { debug_mode = 1; idevice_set_debug_level(1); socket_set_verbose(3); continue; } else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { i++; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); return 0; } udid = argv[i]; continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); return EXIT_SUCCESS; } else if (atoi(argv[i]) > 0) { local_port = atoi(argv[i]); continue; } else { print_usage(argc, argv); return EXIT_SUCCESS; } } /* a PORT is mandatory */ if (!local_port) { fprintf(stderr, "Please specify a PORT.\n"); print_usage(argc, argv); goto leave_cleanup; } /* start services and connect to device */ ret = idevice_new(&device, udid); if (ret != IDEVICE_E_SUCCESS) { if (udid) { fprintf(stderr, "No device found with udid %s, is it plugged in?\n", udid); } else { fprintf(stderr, "No device found, is it plugged in?\n"); } result = EXIT_FAILURE; goto leave_cleanup; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lockdown, "idevicedebugserverproxy")) { fprintf(stderr, "Could not connect to lockdownd. Exiting.\n"); result = EXIT_FAILURE; goto leave_cleanup; } if ((lockdownd_start_service(lockdown, "com.apple.debugserver", &service) != LOCKDOWN_E_SUCCESS) || !service || !service->port) { fprintf(stderr, "Could not start com.apple.debugserver!\nPlease make sure to mount the developer disk image first.\n"); result = EXIT_FAILURE; goto leave_cleanup; } if (idevice_connect(device, service->port, &connection) != IDEVICE_E_SUCCESS) { fprintf(stderr, "Connection to debugserver port %d failed!\n", (int)service->port); result = EXIT_FAILURE; goto leave_cleanup; } /* free lockdown connection if running as it is not needed anymore */ if (lockdown) { lockdownd_client_free(lockdown); lockdown = NULL; } /* setup and create socket endpoint */ socket_info_t socket_info; socket_info.device_connection = connection; socket_info.local_port = local_port; socket_info.remote_port = service->port; if (service) { lockdownd_service_descriptor_free(service); service = NULL; } /* create local socket */ socket_info.server_fd = socket_create(socket_info.local_port); if (socket_info.server_fd < 0) { fprintf(stderr, "Could not create socket\n"); result = EXIT_FAILURE; goto leave_cleanup; } while (!quit_flag) { debug("%s: Waiting for connection on local port %d\n", __func__, socket_info.local_port); /* wait for client */ socket_info.client_fd = socket_accept(socket_info.server_fd, socket_info.local_port); if (socket_info.client_fd < 0) { debug("%s: Continuing...\n", __func__); continue; } debug("%s: Handling new client connection...\n", __func__); if (thread_create(&th, connection_handler, (void*)&socket_info) != 0) { fprintf(stderr, "Could not start connection handler.\n"); socket_shutdown(socket_info.server_fd, SHUT_RDWR); socket_close(socket_info.server_fd); } } debug("%s: Shutting down debugserver proxy...\n", __func__); leave_cleanup: if (connection) { idevice_disconnect(connection); } if (lockdown) { lockdownd_client_free(lockdown); } if (device) { idevice_free(device); } return result; }
int main(int argc, char *argv[]) { lockdownd_client_t client = NULL; lockdownd_service_descriptor_t service = NULL; idevice_t device = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; int i; int op = -1; const char* udid = NULL; const char* param = NULL; /* parse cmdline args */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { idevice_set_debug_level(1); continue; } else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { i++; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); return 0; } udid = argv[i]; continue; } else if (!strcmp(argv[i], "install")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_INSTALL; continue; } else if (!strcmp(argv[i], "list")) { op = OP_LIST; } else if (!strcmp(argv[i], "copy")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_COPY; continue; } else if (!strcmp(argv[i], "remove")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_REMOVE; continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); return 0; } else { print_usage(argc, argv); return 0; } } if ((op == -1) || (op >= NUM_OPS)) { print_usage(argc, argv); return 0; } ret = idevice_new(&device, udid); if (ret != IDEVICE_E_SUCCESS) { if (udid) { printf("No device found with udid %s, is it plugged in?\n", udid); } else { printf("No device found, is it plugged in?\n"); } return -1; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &client, "ideviceprovision")) { idevice_free(device); return -1; } if (LOCKDOWN_E_SUCCESS != lockdownd_start_service(client, "com.apple.misagent", &service)) { fprintf(stderr, "Could not start service \"com.apple.misagent\"\n"); lockdownd_client_free(client); idevice_free(device); return -1; } lockdownd_client_free(client); client = NULL; misagent_client_t mis = NULL; if (misagent_client_new(device, service, &mis) != MISAGENT_E_SUCCESS) { fprintf(stderr, "Could not connect to \"com.apple.misagent\" on device\n"); if (service) lockdownd_service_descriptor_free(service); lockdownd_client_free(client); idevice_free(device); return -1; } if (service) lockdownd_service_descriptor_free(service); switch (op) { case OP_INSTALL: { FILE* f = fopen(param, "rb"); if (!f) { fprintf(stderr, "Could not open file '%s'\n", param); break; } fseek(f, 0, SEEK_END); long int size = ftell(f); fseek(f, 0, SEEK_SET); if (size >= 0x1000000) { fprintf(stderr, "The file '%s' is too large for processing.\n", param); fclose(f); break; } char* buf = (char *)malloc(size); if (!buf) { fprintf(stderr, "Could not allocate memory...\n"); fclose(f); break; } long int cur = 0; while (cur < size) { ssize_t r = fread(buf+cur, 1, 512, f); if (r <= 0) { break; } cur += r; } fclose(f); if (cur != size) { free(buf); fprintf(stderr, "Could not read in file '%s' (size %ld read %ld)\n", param, size, cur); break; } uint64_t psize = size; plist_t pdata = plist_new_data(buf, psize); if (misagent_install(mis, pdata) == MISAGENT_E_SUCCESS) { printf("Profile '%s' installed successfully.\n", param); } else { int sc = misagent_get_status_code(mis); fprintf(stderr, "Could not install profile '%s', status code: 0x%x\n", param, sc); } free(buf); } break; case OP_LIST: case OP_COPY: { plist_t profiles = NULL; if (misagent_copy(mis, &profiles) == MISAGENT_E_SUCCESS) { uint32_t num_profiles = plist_array_get_size(profiles); printf("Device has %d provisioning %s installed:\n", num_profiles, (num_profiles == 1) ? "profile" : "profiles"); uint32_t j; for (j = 0; j < num_profiles; j++) { char* p_name = NULL; char* p_uuid = NULL; plist_t profile = plist_array_get_item(profiles, j); plist_t pl = profile_get_embedded_plist(profile); if (pl && (plist_get_node_type(pl) == PLIST_DICT)) { plist_t node; node = plist_dict_get_item(pl, "Name"); if (node && (plist_get_node_type(node) == PLIST_STRING)) { plist_get_string_val(node, &p_name); } node = plist_dict_get_item(pl, "UUID"); if (node && (plist_get_node_type(node) == PLIST_STRING)) { plist_get_string_val(node, &p_uuid); } } printf("%s - %s\n", (p_uuid) ? p_uuid : "(unknown id)", (p_name) ? p_name : "(no name)"); if (op == OP_COPY) { char pfname[512]; if (p_uuid) { sprintf(pfname, "%s/%s.mobileprovision", param, p_uuid); } else { sprintf(pfname, "%s/profile%d.mobileprovision", param, j); } FILE* f = fopen(pfname, "wb"); if (f) { char* dt = NULL; uint64_t ds = 0; plist_get_data_val(profile, &dt, &ds); fwrite(dt, 1, (size_t) ds, f); fclose(f); printf(" => %s\n", pfname); } else { fprintf(stderr, "Could not open '%s' for writing\n", pfname); } } if (p_uuid) { free(p_uuid); } if (p_name) { free(p_name); } } } else { int sc = misagent_get_status_code(mis); fprintf(stderr, "Could not get installed profiles from device, status code: 0x%x\n", sc); } } break; case OP_REMOVE: if (misagent_remove(mis, param) == MISAGENT_E_SUCCESS) { printf("Profile '%s' removed.\n", param); } else { int sc = misagent_get_status_code(mis); fprintf(stderr, "Could not remove profile '%s', status code 0x%x\n", param, sc); } break; default: break; } misagent_client_free(mis); idevice_free(device); return 0; }
int main(int argc, char** argv) { int res = -1; char* udid = NULL; int c = 0; int optidx = 0; const struct option longopts[] = { { "udid", required_argument, NULL, 'u' }, { "help", no_argument, NULL, 'h' }, { NULL, 0, NULL, 0} }; while ((c = getopt_long(argc, argv, "du:h", longopts, &optidx)) != -1) { switch (c) { case 'u': udid = strdup(optarg); break; case 'h': print_usage(); return 0; case 'd': idevice_set_debug_level(1); break; default: print_usage(); return -1; } } argc -= optind; argv += optind; if (argc > 1) { print_usage(); return -1; } idevice_t device = NULL; if (idevice_new(&device, udid) != IDEVICE_E_SUCCESS) { fprintf(stderr, "ERROR: Could not connect to device\n"); return -1; } lockdownd_client_t lockdown = NULL; lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, "idevicename"); if (lerr != LOCKDOWN_E_SUCCESS) { idevice_free(device); fprintf(stderr, "ERROR: lockdown connection failed, lockdown error %d\n", lerr); return -1; } if (argc == 0) { // getting device name char* name = NULL; lerr = lockdownd_get_device_name(lockdown, &name); if (name) { printf("%s\n", name); free(name); res = 0; } else { fprintf(stderr, "ERROR: Could not get device name, lockdown error %d\n", lerr); } } else { // setting device name lerr = lockdownd_set_value(lockdown, NULL, "DeviceName", plist_new_string(argv[0])); if (lerr == LOCKDOWN_E_SUCCESS) { printf("device name set to '%s'\n", argv[0]); res = 0; } else { fprintf(stderr, "ERROR: Could not set device name, lockdown error %d\n", lerr); } } lockdownd_client_free(lockdown); idevice_free(device); if (udid) { free(udid); } return res; }
int main(int argc, char *argv[]) { lockdownd_client_t client = NULL; lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; lockdownd_service_descriptor_t service = NULL; idevice_t device = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; int i; int op = -1; int output_xml = 0; const char* udid = NULL; const char* param = NULL; /* parse cmdline args */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { idevice_set_debug_level(1); continue; } else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { i++; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); return 0; } udid = argv[i]; continue; } else if (!strcmp(argv[i], "install")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_INSTALL; continue; } else if (!strcmp(argv[i], "list")) { op = OP_LIST; } else if (!strcmp(argv[i], "copy")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_COPY; continue; } else if (!strcmp(argv[i], "remove")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_REMOVE; continue; } else if (!strcmp(argv[i], "dump")) { i++; if (!argv[i] || (strlen(argv[i]) < 1)) { print_usage(argc, argv); return 0; } param = argv[i]; op = OP_DUMP; continue; } else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) { output_xml = 1; continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); return 0; } else { print_usage(argc, argv); return 0; } } if ((op == -1) || (op >= NUM_OPS)) { print_usage(argc, argv); return 0; } if (op == OP_DUMP) { int res = 0; unsigned char* profile_data = NULL; unsigned int profile_size = 0; if (profile_read_from_file(param, &profile_data, &profile_size) != 0) { return -1; } plist_t pdata = plist_new_data((char*)profile_data, profile_size); plist_t pl = profile_get_embedded_plist(pdata); plist_free(pdata); free(profile_data); if (pl) { if (output_xml) { char* xml = NULL; uint32_t xlen = 0; plist_to_xml(pl, &xml, &xlen); if (xml) { printf("%s\n", xml); free(xml); } } else { if (pl && (plist_get_node_type(pl) == PLIST_DICT)) { plist_print_to_stream(pl, stdout); } else { fprintf(stderr, "ERROR: unexpected node type in profile plist (not PLIST_DICT)\n"); res = -1; } } } else { fprintf(stderr, "ERROR: could not extract embedded plist from profile!\n"); } plist_free(pl); return res; } ret = idevice_new(&device, udid); if (ret != IDEVICE_E_SUCCESS) { if (udid) { printf("No device found with udid %s, is it plugged in?\n", udid); } else { printf("No device found, is it plugged in?\n"); } return -1; } if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, "ideviceprovision"))) { fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ldret); idevice_free(device); return -1; } if (LOCKDOWN_E_SUCCESS != lockdownd_start_service(client, "com.apple.misagent", &service)) { fprintf(stderr, "Could not start service \"com.apple.misagent\"\n"); lockdownd_client_free(client); idevice_free(device); return -1; } lockdownd_client_free(client); client = NULL; misagent_client_t mis = NULL; if (misagent_client_new(device, service, &mis) != MISAGENT_E_SUCCESS) { fprintf(stderr, "Could not connect to \"com.apple.misagent\" on device\n"); if (service) lockdownd_service_descriptor_free(service); lockdownd_client_free(client); idevice_free(device); return -1; } if (service) lockdownd_service_descriptor_free(service); switch (op) { case OP_INSTALL: { unsigned char* profile_data = NULL; unsigned int profile_size = 0; if (profile_read_from_file(param, &profile_data, &profile_size) != 0) { break; } uint64_t psize = profile_size; plist_t pdata = plist_new_data((const char*)profile_data, psize); free(profile_data); if (misagent_install(mis, pdata) == MISAGENT_E_SUCCESS) { printf("Profile '%s' installed successfully.\n", param); } else { int sc = misagent_get_status_code(mis); fprintf(stderr, "Could not install profile '%s', status code: 0x%x\n", param, sc); } } break; case OP_LIST: case OP_COPY: { plist_t profiles = NULL; if (misagent_copy(mis, &profiles) == MISAGENT_E_SUCCESS) { uint32_t num_profiles = plist_array_get_size(profiles); printf("Device has %d provisioning %s installed:\n", num_profiles, (num_profiles == 1) ? "profile" : "profiles"); uint32_t j; for (j = 0; j < num_profiles; j++) { char* p_name = NULL; char* p_uuid = NULL; plist_t profile = plist_array_get_item(profiles, j); plist_t pl = profile_get_embedded_plist(profile); if (pl && (plist_get_node_type(pl) == PLIST_DICT)) { plist_t node; node = plist_dict_get_item(pl, "Name"); if (node && (plist_get_node_type(node) == PLIST_STRING)) { plist_get_string_val(node, &p_name); } node = plist_dict_get_item(pl, "UUID"); if (node && (plist_get_node_type(node) == PLIST_STRING)) { plist_get_string_val(node, &p_uuid); } } printf("%s - %s\n", (p_uuid) ? p_uuid : "(unknown id)", (p_name) ? p_name : "(no name)"); if (op == OP_COPY) { char pfname[512]; if (p_uuid) { sprintf(pfname, "%s/%s.mobileprovision", param, p_uuid); } else { sprintf(pfname, "%s/profile%d.mobileprovision", param, j); } FILE* f = fopen(pfname, "wb"); if (f) { char* dt = NULL; uint64_t ds = 0; plist_get_data_val(profile, &dt, &ds); fwrite(dt, 1, ds, f); fclose(f); printf(" => %s\n", pfname); } else { fprintf(stderr, "Could not open '%s' for writing\n", pfname); } } if (p_uuid) { free(p_uuid); } if (p_name) { free(p_name); } } } else { int sc = misagent_get_status_code(mis); fprintf(stderr, "Could not get installed profiles from device, status code: 0x%x\n", sc); } } break; case OP_REMOVE: if (misagent_remove(mis, param) == MISAGENT_E_SUCCESS) { printf("Profile '%s' removed.\n", param); } else { int sc = misagent_get_status_code(mis); fprintf(stderr, "Could not remove profile '%s', status code 0x%x\n", param, sc); } break; default: break; } misagent_client_free(mis); idevice_free(device); return 0; }
int main(int argc, char **argv) { idevice_t device = NULL; lockdownd_client_t lckd = NULL; mobile_image_mounter_client_t mim = NULL; afc_client_t afc = NULL; lockdownd_service_descriptor_t service = NULL; int res = -1; char *image_path = NULL; char *image_sig_path = NULL; parse_opts(argc, argv); argc -= optind; argv += optind; if (!list_mode) { if (argc < 1) { printf("ERROR: No IMAGE_FILE has been given!\n"); return -1; } image_path = strdup(argv[0]); if (argc >= 2) { image_sig_path = strdup(argv[1]); } else { if (asprintf(&image_sig_path, "%s.signature", image_path) < 0) { printf("Out of memory?!\n"); return -1; } } } if (IDEVICE_E_SUCCESS != idevice_new(&device, udid)) { printf("No device found, is it plugged in?\n"); return -1; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, "ideviceimagemounter")) { printf("ERROR: could not connect to lockdown. Exiting.\n"); goto leave; } lockdownd_start_service(lckd, "com.apple.mobile.mobile_image_mounter", &service); if (!service || service->port == 0) { printf("ERROR: Could not start mobile_image_mounter service!\n"); goto leave; } if (mobile_image_mounter_new(device, service, &mim) != MOBILE_IMAGE_MOUNTER_E_SUCCESS) { printf("ERROR: Could not connect to mobile_image_mounter!\n"); goto leave; } if (service) { lockdownd_service_descriptor_free(service); service = NULL; } if (!list_mode) { struct stat fst; if ((lockdownd_start_service(lckd, "com.apple.afc", &service) != LOCKDOWN_E_SUCCESS) || !service || !service->port) { fprintf(stderr, "Could not start com.apple.afc!\n"); goto leave; } if (afc_client_new(device, service, &afc) != AFC_E_SUCCESS) { fprintf(stderr, "Could not connect to AFC!\n"); goto leave; } if (service) { lockdownd_service_descriptor_free(service); service = NULL; } if (stat(image_path, &fst) != 0) { fprintf(stderr, "ERROR: stat: %s: %s\n", image_path, strerror(errno)); goto leave; } if (stat(image_sig_path, &fst) != 0) { fprintf(stderr, "ERROR: stat: %s: %s\n", image_sig_path, strerror(errno)); goto leave; } } lockdownd_client_free(lckd); lckd = NULL; mobile_image_mounter_error_t err; plist_t result = NULL; if (list_mode) { /* list mounts mode */ if (!imagetype) { imagetype = strdup("Developer"); } err = mobile_image_mounter_lookup_image(mim, imagetype, &result); free(imagetype); if (err == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { res = 0; if (xml_mode) { print_xml(result); } else { plist_dict_to_string(result); } } else { printf("Error: lookup_image returned %d\n", err); } } else { char sig[8192]; size_t sig_length = 0; FILE *f = fopen(image_sig_path, "rb"); if (!f) { fprintf(stderr, "Error opening signature file '%s': %s\n", image_sig_path, strerror(errno)); goto leave; } sig_length = fread(sig, 1, sizeof(sig), f); fclose(f); if (sig_length == 0) { fprintf(stderr, "Could not read signature from file '%s'\n", image_sig_path); goto leave; } f = fopen(image_path, "rb"); if (!f) { fprintf(stderr, "Error opening image file '%s': %s\n", image_path, strerror(errno)); goto leave; } char *targetname = NULL; if (asprintf(&targetname, "%s/%s", PKG_PATH, "staging.dimage") < 0) { fprintf(stderr, "Out of memory!?\n"); goto leave; } char *mountname = NULL; if (asprintf(&mountname, "%s/%s", PATH_PREFIX, targetname) < 0) { fprintf(stderr, "Out of memory!?\n"); goto leave; } printf("Copying '%s' --> '%s'\n", image_path, targetname); char **strs = NULL; if (afc_get_file_info(afc, PKG_PATH, &strs) != AFC_E_SUCCESS) { if (afc_make_directory(afc, PKG_PATH) != AFC_E_SUCCESS) { fprintf(stderr, "WARNING: Could not create directory '%s' on device!\n", PKG_PATH); } } if (strs) { int i = 0; while (strs[i]) { free(strs[i]); i++; } free(strs); } uint64_t af = 0; if ((afc_file_open(afc, targetname, AFC_FOPEN_WRONLY, &af) != AFC_E_SUCCESS) || !af) { fclose(f); fprintf(stderr, "afc_file_open on '%s' failed!\n", targetname); goto leave; } char buf[8192]; size_t amount = 0; do { amount = fread(buf, 1, sizeof(buf), f); if (amount > 0) { uint32_t written, total = 0; while (total < amount) { written = 0; if (afc_file_write(afc, af, buf, amount, &written) != AFC_E_SUCCESS) { fprintf(stderr, "AFC Write error!\n"); break; } total += written; } if (total != amount) { fprintf(stderr, "Error: wrote only %d of %d\n", total, (unsigned int)amount); afc_file_close(afc, af); fclose(f); goto leave; } } } while (amount > 0); afc_file_close(afc, af); fclose(f); printf("done.\n"); printf("Mounting...\n"); if (!imagetype) { imagetype = strdup("Developer"); } err = mobile_image_mounter_mount_image(mim, mountname, sig, sig_length, imagetype, &result); free(imagetype); if (err == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { if (result) { plist_t node = plist_dict_get_item(result, "Status"); if (node) { char *status = NULL; plist_get_string_val(node, &status); if (status) { if (!strcmp(status, "Complete")) { printf("Done.\n"); res = 0; } else { printf("unexpected status value:\n"); if (xml_mode) { print_xml(result); } else { plist_dict_to_string(result); } } free(status); } else { printf("unexpected result:\n"); if (xml_mode) { print_xml(result); } else { plist_dict_to_string(result); } } } node = plist_dict_get_item(result, "Error"); if (node) { char *error = NULL; plist_get_string_val(node, &error); if (error) { printf("Error: %s\n", error); free(error); } else { printf("unexpected result:\n"); if (xml_mode) { print_xml(result); } else { plist_dict_to_string(result); } } } else { if (xml_mode) { print_xml(result); } else { plist_dict_to_string(result); } } } } else { printf("Error: mount_image returned %d\n", err); } } if (result) { plist_free(result); } /* perform hangup command */ mobile_image_mounter_hangup(mim); /* free client */ mobile_image_mounter_free(mim); leave: if (afc) { afc_client_free(afc); } if (lckd) { lockdownd_client_free(lckd); } idevice_free(device); if (image_path) free(image_path); if (image_sig_path) free(image_sig_path); return res; }
int main(int argc, char **argv) { idevice_t device = NULL; lockdownd_client_t client = NULL; char **dev_list = NULL; char *devname = NULL; int ret = 0; int i; int mode = MODE_SHOW_ID; const char* udid = NULL; /* parse cmdline args */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { idevice_set_debug_level(1); continue; } else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list")) { mode = MODE_LIST_DEVICES; continue; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { print_usage(argc, argv); return 0; } } /* check if udid was passed */ if (mode == MODE_SHOW_ID) { i--; if (!argv[i] || (strlen(argv[i]) != 40)) { print_usage(argc, argv); return 0; } udid = argv[i]; } switch (mode) { case MODE_SHOW_ID: idevice_new(&device, udid); if (!device) { fprintf(stderr, "ERROR: No device with UDID=%s attached.\n", udid); return -2; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(device, &client, "idevice_id")) { idevice_free(device); fprintf(stderr, "ERROR: Connecting to device failed!\n"); return -2; } if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &devname)) || !devname) { fprintf(stderr, "ERROR: Could not get device name!\n"); ret = -2; } lockdownd_client_free(client); idevice_free(device); if (ret == 0) { printf("%s\n", devname); } if (devname) { free(devname); } return ret; case MODE_LIST_DEVICES: default: if (idevice_get_device_list(&dev_list, &i) < 0) { fprintf(stderr, "ERROR: Unable to retrieve device list!\n"); return -1; } for (i = 0; dev_list[i] != NULL; i++) { printf("%s\n", dev_list[i]); } idevice_device_list_free(dev_list); return 0; } }
G_GNUC_INTERNAL char * read_sysinfo_extended_by_uuid (const char *uuid) { lockdownd_client_t client = NULL; idevice_t device = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; char *xml = NULL; char *str = NULL; char *gxml; uint32_t xml_length = 0; plist_t value = NULL; plist_t global = NULL; plist_t ptr = NULL; int cnt = 0; /* usbmuxd needs some time to start up so we try several times * to open the device before finally returning with an error */ while (cnt++ < 20) { ret = idevice_new(&device, uuid); if (ret == IDEVICE_E_SUCCESS) { break; } if (ret != IDEVICE_E_NO_DEVICE) { break; } g_usleep (G_USEC_PER_SEC / 2); } if (ret != IDEVICE_E_SUCCESS) { printf("No device found with uuid %s, is it plugged in?\n", uuid); return NULL; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &client, "libgpod")) { idevice_free(device); return NULL; } /* run query and get format plist */ lockdownd_get_value(client, NULL, NULL, &global); lockdownd_get_value(client, "com.apple.mobile.iTunes", NULL, &value); /* add some required values manually to emulate old plist format */ ptr = plist_dict_get_item(global, "SerialNumber"); plist_get_string_val(ptr, &str); if (str != NULL) { ptr = plist_new_string(str); plist_dict_insert_item(value, "SerialNumber", ptr); free(str); } ptr = plist_dict_get_item(global, "ProductVersion"); plist_get_string_val(ptr, &str); if (str != NULL) { ptr = plist_new_string(str); plist_dict_insert_item(value, "VisibleBuildID", ptr); free(str); } ptr = plist_new_string(uuid); plist_dict_insert_item(value, "FireWireGUID", ptr); ptr = plist_new_string(uuid); plist_dict_insert_item(value, "UniqueDeviceID", ptr); plist_to_xml(value, &xml, &xml_length); ptr = NULL; if (value) plist_free(value); value = NULL; if (global) plist_free(global); global = NULL; lockdownd_client_free(client); idevice_free(device); /* Jump through hoops since libxml will say to free mem it allocated * with xmlFree while memory freed with g_free has to be allocated * by glib. */ if (xml != NULL) { gxml = g_strdup(xml); xmlFree(xml); } else { gxml = NULL; } return gxml; }
int itdb_iphone_start_sync(Itdb_Device *device, void **prepdata) { int res = 0; int sync_starting = 0; itdbprep_t pdata_loc = NULL; const char *uuid; lockdownd_client_t client = NULL; #ifdef HAVE_LIBIMOBILEDEVICE_1_1_5 lockdownd_service_descriptor_t service = NULL; #else uint16_t afcport = 0; #endif int i; uuid = itdb_device_get_uuid (device); if (!uuid) { fprintf(stderr, "Couldn't find get device UUID itdbprep processing won't work!"); return -ENODEV; } printf("libitdbprep: %s called with uuid=%s\n", __func__, uuid); *prepdata = NULL; pdata_loc = g_new0 (struct itdbprep_int, 1); res = idevice_new(&pdata_loc->device, uuid); if (IDEVICE_E_SUCCESS != res) { fprintf(stderr, "No iPhone found, is it plugged in?\n"); res = -ENODEV; goto leave_with_err; } if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(pdata_loc->device, &client, "libgpod")) { fprintf(stderr, "Error: Could not establish lockdownd connection!\n"); res = -1; goto leave_with_err; } #ifdef HAVE_LIBIMOBILEDEVICE_1_1_5 lockdownd_start_service(client, "com.apple.afc", &service); if (!service || !service->port) { fprintf(stderr, "Error: Could not start AFC service!\n"); res = -1; goto leave_with_err; } afc_client_new(pdata_loc->device, service, &pdata_loc->afc); #else lockdownd_start_service(client, "com.apple.afc", &afcport); if (!afcport) { fprintf(stderr, "Error: Could not start AFC service!\n"); res = -1; goto leave_with_err; } afc_client_new(pdata_loc->device, afcport, &pdata_loc->afc); #endif if (!pdata_loc->afc) { fprintf(stderr, "Error: Could not start AFC client!\n"); res = -1; goto leave_with_err; } if (itdb_iphone_post_notification(pdata_loc->device, client, NP_SYNC_WILL_START)) { fprintf(stderr, "could not post syncWillStart notification!\n"); res = -1; goto leave_with_err; } printf("%s: posted syncWillStart\n", __func__); sync_starting = 1; /* OPEN AND LOCK /com.apple.itunes.lock_sync */ afc_file_open(pdata_loc->afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &pdata_loc->lockfile); if (!pdata_loc->lockfile) { fprintf(stderr, "could not open lockfile\n"); res = -1; goto leave_with_err; } if (itdb_iphone_post_notification(pdata_loc->device, client, "com.apple.itunes-mobdev.syncLockRequest")) { fprintf(stderr, "could not post syncLockRequest\n"); res = -1; goto leave_with_err; } printf("%s: posted syncLockRequest\n", __func__); for (i=0; i<LOCK_ATTEMPTS; i++) { fprintf(stderr, "Locking for sync, attempt %d...\n", i); res = afc_file_lock(pdata_loc->afc, pdata_loc->lockfile, AFC_LOCK_EX); if (res == AFC_E_SUCCESS) { break; } else if (res == AFC_E_OP_WOULD_BLOCK) { usleep(LOCK_WAIT); continue; } else { fprintf(stderr, "ERROR: could not lock file! error code: %d\n", res); res = -1; goto leave_with_err; } } if (i == LOCK_ATTEMPTS) { fprintf(stderr, "ERROR: timeout while locking for sync\n"); res = -1; goto leave_with_err; } if (itdb_iphone_post_notification(pdata_loc->device, client, NP_SYNC_DID_START)) { fprintf(stderr, "could not post syncDidStart\n"); res = -1; goto leave_with_err; } printf("%s: posted syncDidStart\n", __func__); lockdownd_client_free(client); client = NULL; *prepdata = pdata_loc; return 0; leave_with_err: if (client && sync_starting) { itdb_iphone_post_notification(pdata_loc->device, client, "com.apple.itunes-mobdev.syncFailedToStart"); printf("%s: posted syncFailedToStart\n", __func__); } if (pdata_loc) { if (pdata_loc->afc) { if (pdata_loc->lockfile) { afc_file_lock(pdata_loc->afc, pdata_loc->lockfile, AFC_LOCK_UN); afc_file_close(pdata_loc->afc, pdata_loc->lockfile); pdata_loc->lockfile = 0; } afc_client_free(pdata_loc->afc); pdata_loc->afc = NULL; } if (pdata_loc->device) { idevice_free(pdata_loc->device); pdata_loc->device = NULL; } g_free(pdata_loc); pdata_loc = NULL; } if (client) { lockdownd_client_free(client); client = NULL; } *prepdata = NULL; return res; }
int main(int argc, char **argv) { idevice_t phone = NULL; lockdownd_client_t client = NULL; uint16_t port = 0; char* crashLogFile; if(argc != 2) { print_usage(argc, argv); return -1; } crashLogFile = argv[1]; if (idevice_new(&phone, NULL) != IDEVICE_E_SUCCESS) { printf("No device found, is it plugged in?\n"); return -1; } if (lockdownd_client_new_with_handshake(phone, &client, "idevicecrashlog") != LOCKDOWN_E_SUCCESS) { fprintf(stderr, "Could not connect to lockdownd. Exiting.\n"); return -1; } if ((lockdownd_start_service(client, "com.apple.mobile.file_relay", &port) != LOCKDOWN_E_SUCCESS) || !port) { fprintf(stderr, "Could not start com.apple.mobile.file_relay!\n"); return -1; } const char *sources[] = {"CrashReporter", NULL}; idevice_connection_t dump = NULL; file_relay_client_t frc = NULL; if (file_relay_client_new(phone, port, &frc) != FILE_RELAY_E_SUCCESS) { printf("could not connect to file_relay service!\n"); return -1; } if (file_relay_request_sources(frc, sources, &dump) != FILE_RELAY_E_SUCCESS) { printf("could not get sources\n"); return -1; } if (!dump) { printf("did not get connection!\n"); return -1; } uint32_t cnt = 0; uint32_t len = 0; char buf[4096]; char* dumpTmpFile = tmpnam(NULL); FILE *f = fopen(dumpTmpFile, "w"); //receiving file while (idevice_connection_receive(dump, buf, 4096, &len) == IDEVICE_E_SUCCESS) { fwrite(buf, 1, len, f); cnt += len; len = 0; } fclose(f); extract_file(dumpTmpFile, crashLogFile); lockdownd_client_free(client); }