libnm,core: enhance nm_utils_hexstr2bin()
Make the type return GBytes since most in-tree users want that. Allow the function to accept many more formats as valid hex, including bytes delimited by ':' and a leading '0x'.
This commit is contained in:
@@ -709,46 +709,17 @@ GBytes *
|
||||
nm_dhcp_utils_client_id_string_to_bytes (const char *client_id)
|
||||
{
|
||||
GBytes *bytes = NULL;
|
||||
guint i = 0, x = 0;
|
||||
guint len;
|
||||
char *c;
|
||||
int a;
|
||||
|
||||
g_return_val_if_fail (client_id && client_id[0], NULL);
|
||||
|
||||
/* Accept a binary client ID in hex digits with the ':' delimiter,
|
||||
* otherwise treat it as a string.
|
||||
*/
|
||||
len = strlen (client_id);
|
||||
c = g_malloc0 (len / 2 + 1);
|
||||
while (client_id[i]) {
|
||||
a = g_ascii_xdigit_value (client_id[i++]);
|
||||
if (a >= 0) {
|
||||
if (client_id[i] != ':') {
|
||||
c[x] = ((guint8) a << 4);
|
||||
a = g_ascii_xdigit_value (client_id[i++]);
|
||||
}
|
||||
if (a >= 0)
|
||||
c[x++] |= (guint8) a;
|
||||
}
|
||||
if (client_id[i]) {
|
||||
if (client_id[i] != ':' || !client_id[i + 1]) {
|
||||
/* missing or trailing ':' is invalid for hex-format */
|
||||
a = -1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (a < 0) {
|
||||
g_clear_pointer (&c, g_free);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (c) {
|
||||
g_assert (x > 0);
|
||||
bytes = g_bytes_new_take (c, x);
|
||||
} else {
|
||||
/* Try as hex encoded */
|
||||
if (strchr (client_id, ':'))
|
||||
bytes = nm_utils_hexstr2bin (client_id);
|
||||
if (!bytes) {
|
||||
/* Fall back to string */
|
||||
len = strlen (client_id);
|
||||
c = g_malloc (len + 1);
|
||||
c[0] = 0; /* type: non-hardware address per RFC 2132 section 9.14 */
|
||||
memcpy (c + 1, client_id, len);
|
||||
|
@@ -3224,7 +3224,6 @@ make_wireless_setting (shvarFile *ifcfg,
|
||||
GError **error)
|
||||
{
|
||||
NMSettingWireless *s_wireless;
|
||||
GBytes *bytes = NULL;
|
||||
char *value = NULL;
|
||||
gint64 chan = 0;
|
||||
|
||||
@@ -3256,19 +3255,19 @@ make_wireless_setting (shvarFile *ifcfg,
|
||||
|
||||
value = svGetValue (ifcfg, "ESSID", TRUE);
|
||||
if (value) {
|
||||
gsize ssid_len = 0, value_len = strlen (value);
|
||||
char *p = value, *tmp;
|
||||
char buf[33];
|
||||
GBytes *bytes = NULL;
|
||||
gsize ssid_len = 0;
|
||||
gsize value_len = strlen (value);
|
||||
|
||||
ssid_len = value_len;
|
||||
if ( (value_len >= 2)
|
||||
&& (value[0] == '"')
|
||||
&& (value[value_len - 1] == '"')) {
|
||||
/* Strip the quotes and unescape */
|
||||
p = value + 1;
|
||||
char *p = value + 1;
|
||||
|
||||
value[value_len - 1] = '\0';
|
||||
svUnescape (p);
|
||||
ssid_len = strlen (p);
|
||||
bytes = g_bytes_new (p, strlen (p));
|
||||
} else if ((value_len > 2) && (strncmp (value, "0x", 2) == 0)) {
|
||||
/* Hex representation */
|
||||
if (value_len % 2) {
|
||||
@@ -3279,34 +3278,27 @@ make_wireless_setting (shvarFile *ifcfg,
|
||||
goto error;
|
||||
}
|
||||
|
||||
p = value + 2;
|
||||
while (*p) {
|
||||
if (!g_ascii_isxdigit (*p)) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
"Invalid SSID '%s' character (looks like hex SSID but '%c' isn't a hex digit)",
|
||||
value, *p);
|
||||
g_free (value);
|
||||
goto error;
|
||||
}
|
||||
p++;
|
||||
bytes = nm_utils_hexstr2bin (value);
|
||||
if (!bytes) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
"Invalid SSID '%s' (looks like hex SSID but isn't)",
|
||||
value);
|
||||
g_free (value);
|
||||
goto error;
|
||||
}
|
||||
} else
|
||||
bytes = g_bytes_new (value, value_len);
|
||||
|
||||
tmp = nm_utils_hexstr2bin (value + 2, value_len - 2);
|
||||
ssid_len = (value_len - 2) / 2;
|
||||
memcpy (buf, tmp, ssid_len);
|
||||
p = &buf[0];
|
||||
g_free (tmp);
|
||||
}
|
||||
|
||||
ssid_len = g_bytes_get_size (bytes);
|
||||
if (ssid_len > 32 || ssid_len == 0) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
"Invalid SSID '%s' (size %zu not between 1 and 32 inclusive)",
|
||||
value, ssid_len);
|
||||
g_bytes_unref (bytes);
|
||||
g_free (value);
|
||||
goto error;
|
||||
}
|
||||
|
||||
bytes = g_bytes_new (p, ssid_len);
|
||||
g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, bytes, NULL);
|
||||
g_bytes_unref (bytes);
|
||||
g_free (value);
|
||||
|
@@ -43,8 +43,16 @@ connection_id_from_ifnet_name (const char *conn_name)
|
||||
int name_len = strlen (conn_name);
|
||||
|
||||
/* Convert a hex-encoded conn_name (only used for wifi SSIDs) to human-readable one */
|
||||
if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x")))
|
||||
return nm_utils_hexstr2bin (conn_name + 2, name_len - 2);
|
||||
if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x"))) {
|
||||
GBytes *bytes = nm_utils_hexstr2bin (conn_name);
|
||||
char *buf;
|
||||
|
||||
if (bytes) {
|
||||
buf = g_strndup (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
|
||||
g_bytes_unref (bytes);
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
return g_strdup (conn_name);
|
||||
}
|
||||
@@ -882,7 +890,6 @@ make_wireless_connection_setting (const char *conn_name,
|
||||
NMSetting8021x **s_8021x,
|
||||
GError **error)
|
||||
{
|
||||
GBytes *bytes;
|
||||
const char *mac = NULL;
|
||||
NMSettingWireless *wireless_setting = NULL;
|
||||
gboolean adhoc = FALSE;
|
||||
@@ -913,9 +920,8 @@ make_wireless_connection_setting (const char *conn_name,
|
||||
|
||||
/* handle ssid (hex and ascii) */
|
||||
if (conn_name) {
|
||||
GBytes *bytes;
|
||||
gsize ssid_len = 0, value_len = strlen (conn_name);
|
||||
const char *p;
|
||||
char *tmp, *converted = NULL;
|
||||
|
||||
ssid_len = value_len;
|
||||
if ((value_len > 2) && (g_str_has_prefix (conn_name, "0x"))) {
|
||||
@@ -926,32 +932,27 @@ make_wireless_connection_setting (const char *conn_name,
|
||||
conn_name);
|
||||
goto error;
|
||||
}
|
||||
// ignore "0x"
|
||||
p = conn_name + 2;
|
||||
if (!is_hex (p)) {
|
||||
|
||||
bytes = nm_utils_hexstr2bin (conn_name);
|
||||
if (!bytes) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
"Invalid SSID '%s' character (looks like hex SSID but '%c' isn't a hex digit)",
|
||||
conn_name, *p);
|
||||
"Invalid SSID '%s' (looks like hex SSID but isn't)",
|
||||
conn_name);
|
||||
goto error;
|
||||
|
||||
}
|
||||
tmp = nm_utils_hexstr2bin (p, value_len - 2);
|
||||
ssid_len = (value_len - 2) / 2;
|
||||
converted = g_malloc0 (ssid_len + 1);
|
||||
memcpy (converted, tmp, ssid_len);
|
||||
g_free (tmp);
|
||||
}
|
||||
} else
|
||||
bytes = g_bytes_new (conn_name, value_len);
|
||||
|
||||
ssid_len = g_bytes_get_size (bytes);
|
||||
if (ssid_len > 32 || ssid_len == 0) {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
"Invalid SSID '%s' (size %zu not between 1 and 32 inclusive)",
|
||||
conn_name, ssid_len);
|
||||
goto error;
|
||||
}
|
||||
bytes = g_bytes_new (converted ? converted : conn_name, ssid_len);
|
||||
|
||||
g_object_set (wireless_setting, NM_SETTING_WIRELESS_SSID, bytes, NULL);
|
||||
g_bytes_unref (bytes);
|
||||
g_free (converted);
|
||||
} else {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
"Missing SSID");
|
||||
@@ -1095,7 +1096,7 @@ add_one_wep_key (const char *ssid,
|
||||
|
||||
}
|
||||
|
||||
converted = nm_utils_bin2hexstr (tmp, strlen (tmp), strlen (tmp) * 2);
|
||||
converted = nm_utils_bin2hexstr (tmp, strlen (tmp), -1);
|
||||
g_free (tmp);
|
||||
} else {
|
||||
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
|
||||
|
@@ -542,8 +542,8 @@ add_wep_key (NMSupplicantConfig *self,
|
||||
const char *name,
|
||||
NMWepKeyType wep_type)
|
||||
{
|
||||
char *value;
|
||||
gboolean success;
|
||||
GBytes *bytes;
|
||||
gboolean success = FALSE;
|
||||
size_t key_len = key ? strlen (key) : 0;
|
||||
|
||||
if (!key || !key_len)
|
||||
@@ -552,9 +552,15 @@ add_wep_key (NMSupplicantConfig *self,
|
||||
if ( (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
|
||||
|| (wep_type == NM_WEP_KEY_TYPE_KEY)) {
|
||||
if ((key_len == 10) || (key_len == 26)) {
|
||||
value = nm_utils_hexstr2bin (key, strlen (key));
|
||||
success = nm_supplicant_config_add_option (self, name, value, key_len / 2, TRUE);
|
||||
g_free (value);
|
||||
bytes = nm_utils_hexstr2bin (key);
|
||||
if (bytes) {
|
||||
success = nm_supplicant_config_add_option (self,
|
||||
name,
|
||||
g_bytes_get_data (bytes, NULL),
|
||||
g_bytes_get_size (bytes),
|
||||
TRUE);
|
||||
g_bytes_unref (bytes);
|
||||
}
|
||||
if (!success) {
|
||||
nm_log_warn (LOGD_SUPPLICANT, "Error adding %s to supplicant config.", name);
|
||||
return FALSE;
|
||||
@@ -590,8 +596,7 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self,
|
||||
NMSetting8021x *setting_8021x,
|
||||
const char *con_uuid)
|
||||
{
|
||||
char *value;
|
||||
gboolean success;
|
||||
gboolean success = FALSE;
|
||||
const char *key_mgmt, *auth_alg;
|
||||
const char *psk;
|
||||
|
||||
@@ -612,10 +617,18 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self,
|
||||
size_t psk_len = strlen (psk);
|
||||
|
||||
if (psk_len == 64) {
|
||||
GBytes *bytes;
|
||||
|
||||
/* Hex PSK */
|
||||
value = nm_utils_hexstr2bin (psk, psk_len);
|
||||
success = nm_supplicant_config_add_option (self, "psk", value, psk_len / 2, TRUE);
|
||||
g_free (value);
|
||||
bytes = nm_utils_hexstr2bin (psk);
|
||||
if (bytes) {
|
||||
success = nm_supplicant_config_add_option (self,
|
||||
"psk",
|
||||
g_bytes_get_data (bytes, NULL),
|
||||
g_bytes_get_size (bytes),
|
||||
TRUE);
|
||||
g_bytes_unref (bytes);
|
||||
}
|
||||
if (!success) {
|
||||
nm_log_warn (LOGD_SUPPLICANT, "Error adding 'psk' to supplicant config.");
|
||||
return FALSE;
|
||||
@@ -653,6 +666,7 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self,
|
||||
const char *wep1 = nm_setting_wireless_security_get_wep_key (setting, 1);
|
||||
const char *wep2 = nm_setting_wireless_security_get_wep_key (setting, 2);
|
||||
const char *wep3 = nm_setting_wireless_security_get_wep_key (setting, 3);
|
||||
char *value;
|
||||
|
||||
if (!add_wep_key (self, wep0, "wep_key0", wep_type))
|
||||
return FALSE;
|
||||
|
Reference in New Issue
Block a user