[network-manager-openswan/dcbw/cleanups: 10/17] core: simplify writing the PSK
- From: Dan Williams <dcbw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [network-manager-openswan/dcbw/cleanups: 10/17] core: simplify writing the PSK
- Date: Thu, 28 Aug 2014 22:52:38 +0000 (UTC)
commit 58b908ab9dce209016f8c7946a3feb8b18c2d4a4
Author: Dan Williams <dcbw redhat com>
Date: Mon Aug 4 16:07:11 2014 -0500
core: simplify writing the PSK
write_one_property() was only used for the PSK, so just remove it.
src/nm-openswan-service.c | 129 +++++++++------------------------------------
1 files changed, 25 insertions(+), 104 deletions(-)
---
diff --git a/src/nm-openswan-service.c b/src/nm-openswan-service.c
index 30a3f45..e65f3b6 100644
--- a/src/nm-openswan-service.c
+++ b/src/nm-openswan-service.c
@@ -393,80 +393,9 @@ write_config_option (int fd, const char *format, ...)
va_end (args);
}
-typedef struct {
- int conf_fd;
- int secret_fd;
- NMSettingVPN *s_vpn;
- GError *error;
- gboolean upw_ignored;
- gboolean gpw_ignored;
-} WriteConfigInfo;
-
-static void
-write_one_property (const char *key, const char *value, gpointer user_data)
-{
- WriteConfigInfo *info = (WriteConfigInfo *) user_data;
- GType type = G_TYPE_INVALID;
- int i;
- const char *leftid;
-
- if (info->error)
- return;
-
- /* Find the value in the table to get its type */
- for (i = 0; valid_properties[i].name; i++) {
- ValidProperty prop = valid_properties[i];
-
- if (!strcmp (prop.name, (char *) key)) {
- /* Property is ok */
- type = prop.type;
- break;
- }
- }
-
- /* Try the valid secrets table */
- for (i = 0; type == G_TYPE_INVALID && valid_secrets[i].name; i++) {
- ValidProperty prop = valid_secrets[i];
-
- if (!strcmp (prop.name, (char *) key)) {
- /* Property is ok */
- type = prop.type;
- break;
- }
- }
-
- if (type == G_TYPE_INVALID) {
- g_set_error (&info->error,
- NM_VPN_PLUGIN_ERROR,
- NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
- "Config option '%s' invalid or unknown.",
- (const char *) key);
- }
-
- /* Don't write ignored secrets */
- if (!strcmp (key, NM_OPENSWAN_XAUTH_PASSWORD) && info->upw_ignored)
- return;
- if (!strcmp (key, NM_OPENSWAN_PSK_VALUE) && info->gpw_ignored)
- return;
-
- if (type == G_TYPE_STRING) {
- if (!strcmp (key, NM_OPENSWAN_PSK_VALUE)) {
- leftid = nm_setting_vpn_get_data_item (info->s_vpn, NM_OPENSWAN_LEFTID);
- write_config_option (info->secret_fd, "@%s: PSK \"%s\"\n", leftid, (char *) value);
- }
- } else if (type == G_TYPE_NONE) {
- /* ignored */
- } else {
- /* Just ignore unknown properties */
- g_warning ("Don't know how to write property '%s' with type %s",
- (char *) key, g_type_name (type));
- }
-}
-
static gboolean
nm_openswan_config_write (gint fd, NMSettingVPN *s_vpn, GError **error)
{
- WriteConfigInfo *info;
const char *props_username;
const char *default_username;
const char *phase1_alg_str;
@@ -514,48 +443,40 @@ nm_openswan_config_write (gint fd, NMSettingVPN *s_vpn, GError **error)
write_config_option (fd, " keyingtries=1\n");
write_config_option (fd, " auto=add");
- info = g_malloc0 (sizeof (WriteConfigInfo));
- info->conf_fd = fd;
- info->s_vpn = s_vpn;
-
- nm_setting_vpn_foreach_data_item (s_vpn, write_one_property, info);
- *error = info->error;
- close (fd);
- sleep (3);
- g_free (info);
-
- return *error ? FALSE : TRUE;
+ return TRUE;
}
static gboolean
-nm_openswan_config_secret_write (NMSettingVPN *s_vpn, GError **error)
+nm_openswan_config_psk_write (NMSettingVPN *s_vpn, GError **error)
{
- WriteConfigInfo *info;
- const char *pw_type;
- gint secret_fd=-1;
-
- secret_fd = open ("/etc/ipsec.d/ipsec-nm-conn1.secrets", O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
-
- info = g_malloc0 (sizeof (WriteConfigInfo));
- info->secret_fd = secret_fd;
- info->s_vpn = s_vpn;
-
- /* Check for ignored user password */
- pw_type = nm_setting_vpn_get_data_item (s_vpn, NM_OPENSWAN_XAUTH_PASSWORD_INPUT_MODES);
- if (pw_type && !strcmp (pw_type, NM_OPENSWAN_PW_TYPE_UNUSED))
- info->upw_ignored = TRUE;
+ const char *pw_type, *psk, *leftid;
+ int fd;
/* Check for ignored group password */
pw_type = nm_setting_vpn_get_data_item (s_vpn, NM_OPENSWAN_PSK_INPUT_MODES);
if (pw_type && !strcmp (pw_type, NM_OPENSWAN_PW_TYPE_UNUSED))
- info->gpw_ignored = TRUE;
+ return TRUE;
- nm_setting_vpn_foreach_secret (s_vpn, write_one_property, info);
- *error = info->error;
- close (secret_fd);
- g_free (info);
+ psk = nm_setting_vpn_get_secret (s_vpn, NM_OPENSWAN_PSK_VALUE);
+ if (!psk)
+ return TRUE;
- return *error ? FALSE : TRUE;
+ /* Write the PSK */
+ fd = open ("/etc/ipsec.d/ipsec-nm-conn1.secrets", O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
+ if (fd < 0) {
+ g_set_error_literal (error,
+ NM_VPN_PLUGIN_ERROR,
+ NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
+ "Failed to open secrets file.");
+ return FALSE;
+ }
+
+ leftid = nm_setting_vpn_get_data_item (s_vpn, NM_OPENSWAN_LEFTID);
+ g_assert (leftid);
+ write_config_option (fd, "@%s: PSK \"%s\"\n", leftid, psk);
+
+ close (fd);
+ return TRUE;
}
static gboolean
@@ -576,7 +497,7 @@ real_connect (NMVPNPlugin *plugin,
if (!nm_openswan_secrets_validate (s_vpn, error))
goto out;
- if (!nm_openswan_config_secret_write (s_vpn, error))
+ if (!nm_openswan_config_psk_write (s_vpn, error))
goto out;
openswan_fd = nm_openswan_start_openswan_binary (NM_OPENSWAN_PLUGIN (plugin), error);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]