libnm: implement "wireguard.private-key" as direct string property
"wireguard.private-key" is special, because the setter does some unusual normalization. To implement that, we need to use "direct_hook.set_string_func".
This commit is contained in:
@@ -1771,7 +1771,7 @@ verify_secrets(NMSetting *setting, NMConnection *connection, GError **error)
|
|||||||
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(setting);
|
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(setting);
|
||||||
guint i;
|
guint i;
|
||||||
|
|
||||||
if (priv->private_key && !priv->private_key_valid) {
|
if (!priv->private_key_valid) {
|
||||||
g_set_error_literal(error,
|
g_set_error_literal(error,
|
||||||
NM_CONNECTION_ERROR,
|
NM_CONNECTION_ERROR,
|
||||||
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
NM_CONNECTION_ERROR_INVALID_PROPERTY,
|
||||||
@@ -1806,7 +1806,7 @@ need_secrets(NMSetting *setting)
|
|||||||
GPtrArray *secrets = NULL;
|
GPtrArray *secrets = NULL;
|
||||||
guint i;
|
guint i;
|
||||||
|
|
||||||
if (!priv->private_key || !priv->private_key_valid) {
|
if (!priv->private_key_valid) {
|
||||||
secrets = g_ptr_array_new_full(1, g_free);
|
secrets = g_ptr_array_new_full(1, g_free);
|
||||||
g_ptr_array_add(secrets, g_strdup(NM_SETTING_WIREGUARD_PRIVATE_KEY));
|
g_ptr_array_add(secrets, g_strdup(NM_SETTING_WIREGUARD_PRIVATE_KEY));
|
||||||
}
|
}
|
||||||
@@ -2258,53 +2258,35 @@ for_each_secret(NMSetting *setting,
|
|||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
static void
|
static gboolean
|
||||||
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
_set_string_fcn_public_key(const NMSettInfoSetting *sett_info,
|
||||||
|
const NMSettInfoProperty *property_info,
|
||||||
|
NMSetting *setting,
|
||||||
|
const char *str)
|
||||||
{
|
{
|
||||||
NMSettingWireGuard *setting = NM_SETTING_WIREGUARD(object);
|
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(setting);
|
||||||
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(setting);
|
gboolean valid;
|
||||||
|
char *new = NULL;
|
||||||
|
char *old;
|
||||||
|
|
||||||
switch (prop_id) {
|
if (str)
|
||||||
case PROP_PEER_ROUTES:
|
valid = nm_utils_base64secret_normalize(str, NM_WIREGUARD_PUBLIC_KEY_LEN, &new);
|
||||||
g_value_set_boolean(value, priv->peer_routes);
|
else
|
||||||
break;
|
valid = FALSE;
|
||||||
case PROP_PRIVATE_KEY:
|
|
||||||
g_value_set_string(value, priv->private_key);
|
if (nm_streq0(new ?: str, priv->private_key)) {
|
||||||
break;
|
nm_assert(priv->private_key_valid == valid);
|
||||||
default:
|
nm_free_secret(new);
|
||||||
_nm_setting_property_get_property_direct(object, prop_id, value, pspec);
|
return FALSE;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
priv->private_key_valid = valid;
|
||||||
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
|
||||||
{
|
|
||||||
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(object);
|
|
||||||
const char *str;
|
|
||||||
|
|
||||||
switch (prop_id) {
|
old = priv->private_key;
|
||||||
case PROP_PEER_ROUTES:
|
priv->private_key = new ?: g_strdup(str);
|
||||||
priv->peer_routes = g_value_get_boolean(value);
|
nm_free_secret(old);
|
||||||
break;
|
|
||||||
case PROP_PRIVATE_KEY:
|
return TRUE;
|
||||||
nm_clear_pointer(&priv->private_key, nm_free_secret);
|
|
||||||
str = g_value_get_string(value);
|
|
||||||
if (str) {
|
|
||||||
if (nm_utils_base64secret_normalize(str,
|
|
||||||
NM_WIREGUARD_PUBLIC_KEY_LEN,
|
|
||||||
&priv->private_key))
|
|
||||||
priv->private_key_valid = TRUE;
|
|
||||||
else {
|
|
||||||
priv->private_key = g_strdup(str);
|
|
||||||
priv->private_key_valid = FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
_nm_setting_property_set_property_direct(object, prop_id, value, pspec);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
@@ -2338,8 +2320,6 @@ finalize(GObject *object)
|
|||||||
{
|
{
|
||||||
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(object);
|
NMSettingWireGuardPrivate *priv = NM_SETTING_WIREGUARD_GET_PRIVATE(object);
|
||||||
|
|
||||||
nm_free_secret(priv->private_key);
|
|
||||||
|
|
||||||
_peers_clear(priv);
|
_peers_clear(priv);
|
||||||
g_ptr_array_unref(priv->peers_arr);
|
g_ptr_array_unref(priv->peers_arr);
|
||||||
g_hash_table_unref(priv->peers_hash);
|
g_hash_table_unref(priv->peers_hash);
|
||||||
@@ -2354,8 +2334,8 @@ nm_setting_wireguard_class_init(NMSettingWireGuardClass *klass)
|
|||||||
NMSettingClass *setting_class = NM_SETTING_CLASS(klass);
|
NMSettingClass *setting_class = NM_SETTING_CLASS(klass);
|
||||||
GArray *properties_override = _nm_sett_info_property_override_create_array();
|
GArray *properties_override = _nm_sett_info_property_override_create_array();
|
||||||
|
|
||||||
object_class->get_property = get_property;
|
object_class->get_property = _nm_setting_property_get_property_direct;
|
||||||
object_class->set_property = set_property;
|
object_class->set_property = _nm_setting_property_set_property_direct;
|
||||||
object_class->finalize = finalize;
|
object_class->finalize = finalize;
|
||||||
|
|
||||||
setting_class->verify = verify;
|
setting_class->verify = verify;
|
||||||
@@ -2377,12 +2357,15 @@ nm_setting_wireguard_class_init(NMSettingWireGuardClass *klass)
|
|||||||
*
|
*
|
||||||
* Since: 1.16
|
* Since: 1.16
|
||||||
**/
|
**/
|
||||||
obj_properties[PROP_PRIVATE_KEY] =
|
_nm_setting_property_define_direct_string(properties_override,
|
||||||
g_param_spec_string(NM_SETTING_WIREGUARD_PRIVATE_KEY,
|
obj_properties,
|
||||||
"",
|
NM_SETTING_WIREGUARD_PRIVATE_KEY,
|
||||||
"",
|
PROP_PRIVATE_KEY,
|
||||||
NULL,
|
NM_SETTING_PARAM_SECRET,
|
||||||
G_PARAM_READWRITE | NM_SETTING_PARAM_SECRET | G_PARAM_STATIC_STRINGS);
|
NMSettingWireGuardPrivate,
|
||||||
|
private_key,
|
||||||
|
.direct_hook.set_string_fcn =
|
||||||
|
_set_string_fcn_public_key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NMSettingWireGuard:private-key-flags:
|
* NMSettingWireGuard:private-key-flags:
|
||||||
|
Reference in New Issue
Block a user