Skip to content

Commit de7cf07

Browse files
joanbmdkosovic
authored andcommitted
Compatibility with both secret and non-secret PSK
Issue: #78 This restores compatibility with legacy L2TP VPNs with the PSK stored as a non-secret and also migrates the PSK to a secret when editing the connection
1 parent 5de6e5d commit de7cf07

5 files changed

Lines changed: 26 additions & 5 deletions

File tree

auth-dialog/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ get_existing_passwords (GHashTable *vpn_data,
378378
*out_psk = g_strdup (g_hash_table_lookup (existing_secrets, NM_L2TP_KEY_IPSEC_PSK));
379379
if (!*out_psk)
380380
*out_psk = keyring_lookup_secret (vpn_uuid, NM_L2TP_KEY_IPSEC_PSK);
381+
if (!*out_psk) /* For legacy purposes, the PSK can also be specified as a non-secret */
382+
*out_psk = g_strdup (g_hash_table_lookup (vpn_data, NM_L2TP_KEY_IPSEC_PSK));
381383
}
382384
}
383385

properties/ipsec-dialog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "nm-l2tp-editor.h"
1212

1313
#include "nm-utils/nm-shared-utils.h"
14+
#include "nm-utils/nm-secret-utils.h"
1415
#include "shared/nm-l2tp-crypto-openssl.h"
1516
#include "shared/utils.h"
1617

@@ -69,7 +70,7 @@ ipsec_dialog_new_hash_from_connection (NMConnection *connection,
6970
nm_setting_vpn_foreach_data_item (s_vpn, hash_copy_value, hash);
7071

7172
/* IPSEC PSK is special */
72-
secret = nm_setting_vpn_get_secret (s_vpn, NM_L2TP_KEY_IPSEC_PSK);
73+
secret = nm_setting_vpn_get_secret_or_legacy_data_item (s_vpn, NM_L2TP_KEY_IPSEC_PSK);
7374
if (secret) {
7475
g_hash_table_insert (hash,
7576
g_strdup (NM_L2TP_KEY_IPSEC_PSK),

properties/nm-l2tp-editor.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,15 @@ copy_hash_pair (gpointer key, gpointer data, gpointer user_data)
739739
g_return_if_fail (value && value[0]);
740740

741741
/* IPsec PSK and certificate password is a secret, not a data item */
742-
if (!strcmp (key, NM_L2TP_KEY_IPSEC_PSK) || !strcmp (key, NM_L2TP_KEY_MACHINE_CERTPASS))
742+
if (!strcmp (key, NM_L2TP_KEY_IPSEC_PSK)) {
743+
/* Migrate legacy non-secret PSK data items to VPN secret */
744+
nm_setting_vpn_remove_data_item (s_vpn, (const char *) key);
743745
nm_setting_vpn_add_secret (s_vpn, (const char *) key, value);
744-
else
746+
} else if (!strcmp (key, NM_L2TP_KEY_MACHINE_CERTPASS)) {
747+
nm_setting_vpn_add_secret (s_vpn, (const char *) key, value);
748+
} else {
745749
nm_setting_vpn_add_data_item (s_vpn, (const char *) key, value);
750+
}
746751
}
747752

748753
static char *

shared/nm-utils/nm-secret-utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,14 @@ GBytes *nm_secret_buf_to_gbytes_take (NMSecretBuf *secret, gssize actual_len);
133133

134134
/*****************************************************************************/
135135

136+
static inline const char *nm_setting_vpn_get_secret_or_legacy_data_item
137+
(NMSettingVpn *setting, const char *key) {
138+
const char *value = nm_setting_vpn_get_secret (setting, key);
139+
if (!value)
140+
value = nm_setting_vpn_get_data_item (setting, key);
141+
return value;
142+
}
143+
144+
/*****************************************************************************/
145+
136146
#endif /* __NM_SECRET_UTILS_H__ */

src/nm-l2tp-service.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "nm-ppp-status.h"
4040
#include "nm-l2tp-pppd-service-dbus.h"
4141
#include "nm-utils/nm-shared-utils.h"
42+
#include "nm-utils/nm-secret-utils.h"
4243
#include "nm-utils/nm-vpn-plugin-macros.h"
4344
#include "shared/utils.h"
4445
#include "nm-l2tp-crypto-nss.h"
@@ -183,6 +184,8 @@ static const ValidProperty valid_properties[] = {
183184
{ NM_L2TP_KEY_IPSEC_ENABLE, G_TYPE_BOOLEAN, FALSE },
184185
{ NM_L2TP_KEY_IPSEC_REMOTE_ID, G_TYPE_STRING, FALSE },
185186
{ NM_L2TP_KEY_IPSEC_GATEWAY_ID, G_TYPE_STRING, FALSE },
187+
/* For legacy purposes, the PSK can also be specified as a non-secret */
188+
{ NM_L2TP_KEY_IPSEC_PSK, G_TYPE_STRING, FALSE },
186189
{ NM_L2TP_KEY_IPSEC_IKE, G_TYPE_STRING, FALSE },
187190
{ NM_L2TP_KEY_IPSEC_ESP, G_TYPE_STRING, FALSE },
188191
{ NM_L2TP_KEY_IPSEC_IKELIFETIME, G_TYPE_UINT, FALSE },
@@ -747,7 +750,7 @@ nm_l2tp_config_write (NML2tpPlugin *plugin,
747750
}
748751
}
749752

750-
value = nm_setting_vpn_get_secret (s_vpn, NM_L2TP_KEY_IPSEC_PSK);
753+
value = nm_setting_vpn_get_secret_or_legacy_data_item (s_vpn, NM_L2TP_KEY_IPSEC_PSK);
751754
if (!value) value="";
752755

753756
if (g_str_has_prefix (value, "0s")) {
@@ -1966,7 +1969,7 @@ real_need_secrets (NMVpnServicePlugin *plugin,
19661969
need_secrets = TRUE;
19671970

19681971
/* Don't need the PSK if we already have one */
1969-
if (need_secrets && nm_setting_vpn_get_secret (NM_SETTING_VPN (s_vpn), NM_L2TP_KEY_IPSEC_PSK)) {
1972+
if (need_secrets && nm_setting_vpn_get_secret_or_legacy_data_item (NM_SETTING_VPN (s_vpn), NM_L2TP_KEY_IPSEC_PSK)) {
19701973
need_secrets = FALSE;
19711974
}
19721975
}

0 commit comments

Comments
 (0)