mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-08 13:38:43 +02:00
core: add 'dhcp-vendor-class-identifier' validation function
So that it can be reused. Signed-off-by: Antonio Cardace <acardace@redhat.com>
This commit is contained in:
parent
f15c7bbe8d
commit
5cca669ff3
5 changed files with 65 additions and 52 deletions
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
#include "nm-common-macros.h"
|
#include "nm-common-macros.h"
|
||||||
|
|
||||||
|
#include "nm-errors.h"
|
||||||
|
|
||||||
#include <linux/rtnetlink.h>
|
#include <linux/rtnetlink.h>
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
@ -257,3 +259,56 @@ NM_UTILS_ENUM2STR_DEFINE (nm_utils_route_type2str, guint8,
|
||||||
NM_UTILS_ENUM2STR (RTN_UNREACHABLE, "unreachable"),
|
NM_UTILS_ENUM2STR (RTN_UNREACHABLE, "unreachable"),
|
||||||
NM_UTILS_ENUM2STR (RTN_UNSPEC, "unspecified"),
|
NM_UTILS_ENUM2STR (RTN_UNSPEC, "unspecified"),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
nm_utils_validate_dhcp4_vendor_class_id (const char *vci, GError **error)
|
||||||
|
{
|
||||||
|
const char * bin;
|
||||||
|
gsize unescaped_len;
|
||||||
|
gs_free char *to_free = NULL;
|
||||||
|
|
||||||
|
g_return_val_if_fail (!error || !(*error), FALSE);
|
||||||
|
g_return_val_if_fail (vci, FALSE);
|
||||||
|
|
||||||
|
if (vci[0] == '\0') {
|
||||||
|
g_set_error_literal (error,
|
||||||
|
NM_CONNECTION_ERROR,
|
||||||
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
||||||
|
_ ("property cannot be an empty string"));
|
||||||
|
g_prefix_error (error,
|
||||||
|
"%s.%s: ",
|
||||||
|
NM_SETTING_IP4_CONFIG_SETTING_NAME,
|
||||||
|
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
bin = nm_utils_buf_utf8safe_unescape (vci,
|
||||||
|
NM_UTILS_STR_UTF8_SAFE_FLAG_NONE,
|
||||||
|
&unescaped_len,
|
||||||
|
(gpointer *) &to_free);
|
||||||
|
/* a DHCP option cannot be longer than 255 bytes */
|
||||||
|
if (unescaped_len > 255) {
|
||||||
|
g_set_error_literal (error,
|
||||||
|
NM_CONNECTION_ERROR,
|
||||||
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
||||||
|
_ ("property cannot be longer than 255 bytes"));
|
||||||
|
g_prefix_error (error,
|
||||||
|
"%s.%s: ",
|
||||||
|
NM_SETTING_IP4_CONFIG_SETTING_NAME,
|
||||||
|
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if (strlen (bin) != unescaped_len) {
|
||||||
|
g_set_error_literal (error,
|
||||||
|
NM_CONNECTION_ERROR,
|
||||||
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
||||||
|
_ ("property cannot contain any nul bytes"));
|
||||||
|
g_prefix_error (error,
|
||||||
|
"%s.%s: ",
|
||||||
|
NM_SETTING_IP4_CONFIG_SETTING_NAME,
|
||||||
|
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -140,4 +140,6 @@ guint8 nm_utils_route_type_by_name (const char *name);
|
||||||
|
|
||||||
const char *nm_utils_route_type2str (guint8 val, char *buf, gsize len);
|
const char *nm_utils_route_type2str (guint8 val, char *buf, gsize len);
|
||||||
|
|
||||||
|
gboolean nm_utils_validate_dhcp4_vendor_class_id (const char *vci, GError **error);
|
||||||
|
|
||||||
#endif /* __NM_LIBNM_SHARED_UTILS_H__ */
|
#endif /* __NM_LIBNM_SHARED_UTILS_H__ */
|
||||||
|
|
|
||||||
|
|
@ -227,51 +227,9 @@ verify (NMSetting *setting, NMConnection *connection, GError **error)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (priv->dhcp_vendor_class_identifier) {
|
if ( priv->dhcp_vendor_class_identifier
|
||||||
const char * bin;
|
&& !nm_utils_validate_dhcp4_vendor_class_id (priv->dhcp_vendor_class_identifier, error))
|
||||||
gsize unescaped_len;
|
return FALSE;
|
||||||
gs_free char *to_free = NULL;
|
|
||||||
|
|
||||||
if (priv->dhcp_vendor_class_identifier[0] == '\0') {
|
|
||||||
g_set_error_literal (error,
|
|
||||||
NM_CONNECTION_ERROR,
|
|
||||||
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
||||||
_ ("property cannot be an empty string"));
|
|
||||||
g_prefix_error (error,
|
|
||||||
"%s.%s: ",
|
|
||||||
NM_SETTING_IP4_CONFIG_SETTING_NAME,
|
|
||||||
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
bin = nm_utils_buf_utf8safe_unescape (priv->dhcp_vendor_class_identifier,
|
|
||||||
NM_UTILS_STR_UTF8_SAFE_FLAG_NONE,
|
|
||||||
&unescaped_len,
|
|
||||||
(gpointer *) &to_free);
|
|
||||||
/* a DHCP option cannot be longer than 255 bytes */
|
|
||||||
if (unescaped_len > 255) {
|
|
||||||
g_set_error_literal (error,
|
|
||||||
NM_CONNECTION_ERROR,
|
|
||||||
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
||||||
_ ("property cannot be longer than 255 bytes"));
|
|
||||||
g_prefix_error (error,
|
|
||||||
"%s.%s: ",
|
|
||||||
NM_SETTING_IP4_CONFIG_SETTING_NAME,
|
|
||||||
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
if (strlen (bin) != unescaped_len) {
|
|
||||||
g_set_error_literal (error,
|
|
||||||
NM_CONNECTION_ERROR,
|
|
||||||
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
|
||||||
_ ("property cannot contain any nul bytes"));
|
|
||||||
g_prefix_error (error,
|
|
||||||
"%s.%s: ",
|
|
||||||
NM_SETTING_IP4_CONFIG_SETTING_NAME,
|
|
||||||
NM_SETTING_IP4_CONFIG_DHCP_VENDOR_CLASS_IDENTIFIER);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Failures from here on are NORMALIZABLE_ERROR... */
|
/* Failures from here on are NORMALIZABLE_ERROR... */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,7 @@ libnm-core/nm-dbus-utils.c
|
||||||
libnm-core/nm-keyfile/nm-keyfile-utils.c
|
libnm-core/nm-keyfile/nm-keyfile-utils.c
|
||||||
libnm-core/nm-keyfile/nm-keyfile.c
|
libnm-core/nm-keyfile/nm-keyfile.c
|
||||||
libnm-core/nm-libnm-core-aux/nm-libnm-core-aux.c
|
libnm-core/nm-libnm-core-aux/nm-libnm-core-aux.c
|
||||||
|
libnm-core/nm-libnm-core-intern/nm-libnm-core-utils.c
|
||||||
libnm-core/nm-setting-6lowpan.c
|
libnm-core/nm-setting-6lowpan.c
|
||||||
libnm-core/nm-setting-8021x.c
|
libnm-core/nm-setting-8021x.c
|
||||||
libnm-core/nm-setting-adsl.c
|
libnm-core/nm-setting-adsl.c
|
||||||
|
|
|
||||||
|
|
@ -8829,7 +8829,6 @@ dhcp4_get_vendor_class_identifier (NMDevice *self, NMSettingIP4Config *s_ip4)
|
||||||
{
|
{
|
||||||
gs_free char *config_data_prop = NULL;
|
gs_free char *config_data_prop = NULL;
|
||||||
gs_free char *to_free = NULL;
|
gs_free char *to_free = NULL;
|
||||||
gboolean validate = FALSE;
|
|
||||||
const char *conn_prop;
|
const char *conn_prop;
|
||||||
GBytes *bytes = NULL;
|
GBytes *bytes = NULL;
|
||||||
const char *bin;
|
const char *bin;
|
||||||
|
|
@ -8839,12 +8838,14 @@ dhcp4_get_vendor_class_identifier (NMDevice *self, NMSettingIP4Config *s_ip4)
|
||||||
|
|
||||||
if (!conn_prop) {
|
if (!conn_prop) {
|
||||||
/* set in NetworkManager.conf ? */
|
/* set in NetworkManager.conf ? */
|
||||||
validate = TRUE;
|
|
||||||
config_data_prop = nm_config_data_get_connection_default (
|
config_data_prop = nm_config_data_get_connection_default (
|
||||||
NM_CONFIG_GET_DATA,
|
NM_CONFIG_GET_DATA,
|
||||||
NM_CON_DEFAULT ("ipv4.dhcp-vendor-class-identifier"),
|
NM_CON_DEFAULT ("ipv4.dhcp-vendor-class-identifier"),
|
||||||
self);
|
self);
|
||||||
conn_prop = config_data_prop;
|
|
||||||
|
if ( config_data_prop
|
||||||
|
&& nm_utils_validate_dhcp4_vendor_class_id (config_data_prop, NULL))
|
||||||
|
conn_prop = config_data_prop;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (conn_prop) {
|
if (conn_prop) {
|
||||||
|
|
@ -8852,10 +8853,6 @@ dhcp4_get_vendor_class_identifier (NMDevice *self, NMSettingIP4Config *s_ip4)
|
||||||
NM_UTILS_STR_UTF8_SAFE_FLAG_NONE,
|
NM_UTILS_STR_UTF8_SAFE_FLAG_NONE,
|
||||||
&len,
|
&len,
|
||||||
(gpointer *) &to_free);
|
(gpointer *) &to_free);
|
||||||
|
|
||||||
if (validate && (bin[0] == '\0' || len > 255 || strlen (bin) != len))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (to_free)
|
if (to_free)
|
||||||
bytes = g_bytes_new_take (g_steal_pointer (&to_free), len);
|
bytes = g_bytes_new_take (g_steal_pointer (&to_free), len);
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue