From 0bfec4d45b29ac992a30061b8f7190ca8fdc9cbc Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 27 Dec 2013 12:47:26 +0100 Subject: preflight: check for device record before trying to read host id --- src/conf.c | 32 +++++++++++++++++++++++++++++++- src/conf.h | 1 + src/preflight.c | 25 +++++++++++++++---------- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/conf.c b/src/conf.c index 780a7c4..2291671 100644 --- a/src/conf.c +++ b/src/conf.c @@ -325,6 +325,35 @@ static int config_set_system_buid(const char *system_buid) return config_set_value(CONFIG_SYSTEM_BUID_KEY, plist_new_string(system_buid)); } +/** + * Determines whether a pairing record is present for the given device. + * + * @param udid The device UDID as given by the device. + * + * @return 1 if there's a pairing record for the given udid or 0 otherwise. + */ +int config_has_device_record(const char *udid) +{ + int res = 0; + if (!udid) return 0; + + /* ensure config directory exists */ + config_create_config_dir(); + + /* build file path */ + const char *config_path = config_get_config_dir(); + char *device_record_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL); + + struct stat st; + + if ((stat(device_record_file, &st) == 0) && S_ISREG(st.st_mode)) + res = 1; + + free(device_record_file); + + return res; +} + /** * Reads the BUID from a previously generated configuration file. * @@ -430,7 +459,8 @@ int config_get_device_record(const char *udid, char **record_data, uint64_t *rec char *device_record_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL); /* read file */ - buffer_read_from_filename(device_record_file, record_data, record_size); if (!*record_data) { + buffer_read_from_filename(device_record_file, record_data, record_size); + if (!*record_data) { usbmuxd_log(LL_ERROR, "%s: failed to read '%s': %s", __func__, device_record_file, strerror(errno)); res = -ENOENT; } diff --git a/src/conf.h b/src/conf.h index a104185..fd74607 100644 --- a/src/conf.h +++ b/src/conf.h @@ -31,6 +31,7 @@ const char *config_get_config_dir(); void config_get_system_buid(char **system_buid); +int config_has_device_record(const char *udid); int config_get_device_record(const char *udid, char **record_data, uint64_t *record_size); int config_set_device_record(const char *udid, char* record_data, uint64_t record_size); int config_remove_device_record(const char *udid); diff --git a/src/preflight.c b/src/preflight.c index b011344..5198d8c 100644 --- a/src/preflight.c +++ b/src/preflight.c @@ -161,17 +161,22 @@ retry: int is_device_paired = 0; char *host_id = NULL; - config_device_record_get_host_id(dev->udid, &host_id); - lerr = lockdownd_start_session(lockdown, host_id, NULL, NULL); - free(host_id); - if (lerr == LOCKDOWN_E_SUCCESS) { - usbmuxd_log(LL_INFO, "%s: StartSession success for device %s", __func__, _dev->udid); - usbmuxd_log(LL_INFO, "%s: Finished preflight on device %s", __func__, _dev->udid); - client_device_add(info); - goto leave; - } + if (config_has_device_record(dev->udid)) { + config_device_record_get_host_id(dev->udid, &host_id); + lerr = lockdownd_start_session(lockdown, host_id, NULL, NULL); + if (host_id) + free(host_id); + if (lerr == LOCKDOWN_E_SUCCESS) { + usbmuxd_log(LL_INFO, "%s: StartSession success for device %s", __func__, _dev->udid); + usbmuxd_log(LL_INFO, "%s: Finished preflight on device %s", __func__, _dev->udid); + client_device_add(info); + goto leave; + } - usbmuxd_log(LL_INFO, "%s: StartSession failed on device %s, lockdown error %d", __func__, _dev->udid, lerr); + usbmuxd_log(LL_INFO, "%s: StartSession failed on device %s, lockdown error %d", __func__, _dev->udid, lerr); + } else { + lerr = LOCKDOWN_E_INVALID_HOST_ID; + } switch (lerr) { case LOCKDOWN_E_INVALID_HOST_ID: usbmuxd_log(LL_INFO, "%s: Device %s is not paired with this host.", __func__, _dev->udid); -- cgit v1.1-32-gdbae