Add "import connection" feature (has no strong validation)

parent 8ac3474e
...@@ -112,11 +112,104 @@ static VpnImportExportProperty vpn_properties[] = { ...@@ -112,11 +112,104 @@ static VpnImportExportProperty vpn_properties[] = {
{ NULL, G_TYPE_NONE, FALSE } { NULL, G_TYPE_NONE, FALSE }
}; };
/**
* Create new L2TP VPN connection using data from .ini - like file located at #path
*
* Returns: a newly allocated #NMConnection on success or %NULL on failure
**/
NMConnection * NMConnection *
do_import (const char *path, char **lines, GError **error) do_import (const char *path, GError **error)
{ {
return NULL; NMConnection *connection = NULL;
NMSettingConnection *s_con;
NMSettingVPN *s_vpn;
NMSettingIP4Config *s_ip4;
GKeyFile *keyfile;
int i;
keyfile = g_key_file_new ();
if (!g_key_file_load_from_file (keyfile, path, 0, error)) {
g_set_error (error, 0, 0,
_("does not look like a L2TP VPN connection (parse failed)"));
return NULL;
}
connection = nm_connection_new ();
s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
nm_connection_add_setting (connection, NM_SETTING (s_con));
s_vpn = NM_SETTING_VPN (nm_setting_vpn_new ());
g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, NM_DBUS_SERVICE_L2TP, NULL);
nm_connection_add_setting (connection, NM_SETTING (s_vpn));
s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ());
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
/* g_message("Start importing L2TP."); */
for (i = 0; vpn_properties[i].name; i++){
VpnImportExportProperty prop = vpn_properties[i];
int int_val;
gboolean bool_val;
char *value;
if (!g_key_file_has_key (keyfile, VPN_SECTION, prop.name, error)){
if (!prop.required)
continue;
g_set_error (error, 0, 0,
_("Required property %s missing"),
prop.name);
g_key_file_free (keyfile);
g_object_unref (connection);
return NULL;
}
switch (prop.type) {
case G_TYPE_STRING:
value = g_key_file_get_string(keyfile, VPN_SECTION, prop.name, error);
break;
case G_TYPE_UINT:
int_val = g_key_file_get_integer(keyfile, VPN_SECTION, prop.name, error);
if (int_val == 0 && *error){
g_set_error (error, 0, 0,
_("Property %s can't be parsed as integer."),
prop.name);
g_key_file_free (keyfile);
g_object_unref (connection);
return NULL;
}
value = g_strdup_printf ("%d", int_val);
break;
case G_TYPE_BOOLEAN:
bool_val = g_key_file_get_boolean(keyfile, VPN_SECTION, prop.name, error);
if (!bool_val && !(*error))
continue;
if (!bool_val) {
g_set_error (error, 0, 0,
_("Property %s can't be parsed as boolean. Only 'true' and 'false' allowed."),
prop.name);
g_key_file_free (keyfile);
g_object_unref (connection);
return NULL;
}
value = g_strdup("yes");
break;
}
/* TODO: add custom validators for int and string fields there, add special
"validator_flag" field to #vpn_properties and
then use switch "case validator_flag: validation_function() ..." */
/* g_message("Import [%s]%s = %s", VPN_SECTION, prop.name, value); */
nm_setting_vpn_add_data_item (s_vpn, prop.name, value);
g_free (value);
}
/* g_message("Imported L2TP."); */
return connection;
} }
/** /**
...@@ -170,10 +263,12 @@ do_export (const char *path, NMConnection *connection, GError **error) ...@@ -170,10 +263,12 @@ do_export (const char *path, NMConnection *connection, GError **error)
g_key_file_set_string(export_file, VPN_SECTION, prop.name, value); g_key_file_set_string(export_file, VPN_SECTION, prop.name, value);
break; break;
case G_TYPE_BOOLEAN: case G_TYPE_BOOLEAN:
g_key_file_set_boolean(export_file, if (!strcmp(value, "yes"))
VPN_SECTION, g_key_file_set_boolean(export_file,
prop.name, VPN_SECTION,
!strcmp(value, "yes") ? TRUE : FALSE); prop.name,
TRUE);
/* if key not set - assume as "no" */
break; break;
} }
} }
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include <glib.h> #include <glib.h>
#include <nm-connection.h> #include <nm-connection.h>
NMConnection *do_import (const char *path, char **lines, GError **error); NMConnection *do_import (const char *path, GError **error);
gboolean do_export (const char *path, NMConnection *connection, GError **error); gboolean do_export (const char *path, NMConnection *connection, GError **error);
......
...@@ -187,11 +187,9 @@ ipsec_dialog_new_hash_from_dialog (GtkWidget *dialog, GError **error) ...@@ -187,11 +187,9 @@ ipsec_dialog_new_hash_from_dialog (GtkWidget *dialog, GError **error)
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
widget = GTK_WIDGET (gtk_builder_get_object (builder, "ipsec_enable")); widget = GTK_WIDGET (gtk_builder_get_object (builder, "ipsec_enable"));
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) { if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
g_hash_table_insert(hash, g_strdup(NM_L2TP_KEY_IPSEC_ENABLE), g_strdup("yes")); g_hash_table_insert(hash, g_strdup(NM_L2TP_KEY_IPSEC_ENABLE), g_strdup("yes"));
} else {
g_hash_table_insert(hash, g_strdup(NM_L2TP_KEY_IPSEC_ENABLE), g_strdup("no"));
}
widget = GTK_WIDGET (gtk_builder_get_object (builder, "ipsec_gateway_id")); widget = GTK_WIDGET (gtk_builder_get_object (builder, "ipsec_gateway_id"));
g_hash_table_insert(hash, g_strdup(NM_L2TP_KEY_IPSEC_GATEWAY_ID), g_hash_table_insert(hash, g_strdup(NM_L2TP_KEY_IPSEC_GATEWAY_ID),
g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)))); g_strdup(gtk_entry_get_text(GTK_ENTRY(widget))));
......
...@@ -723,8 +723,6 @@ static NMConnection * ...@@ -723,8 +723,6 @@ static NMConnection *
import (NMVpnPluginUiInterface *iface, const char *path, GError **error) import (NMVpnPluginUiInterface *iface, const char *path, GError **error)
{ {
NMConnection *connection = NULL; NMConnection *connection = NULL;
char *contents = NULL;
char **lines = NULL;
char *ext; char *ext;
ext = strrchr (path, '.'); ext = strrchr (path, '.');
...@@ -733,35 +731,27 @@ import (NMVpnPluginUiInterface *iface, const char *path, GError **error) ...@@ -733,35 +731,27 @@ import (NMVpnPluginUiInterface *iface, const char *path, GError **error)
L2TP_PLUGIN_UI_ERROR, L2TP_PLUGIN_UI_ERROR,
L2TP_PLUGIN_UI_ERROR_FILE_NOT_L2TP, L2TP_PLUGIN_UI_ERROR_FILE_NOT_L2TP,
_("unknown L2TP file extension")); _("unknown L2TP file extension"));
goto out; return NULL;
} }
if (strcmp (ext, ".conf") && strcmp (ext, ".cnf")) { if (strcmp (ext, ".conf") && strcmp (ext, ".cnf")) {
g_set_error (error, g_set_error (error,
L2TP_PLUGIN_UI_ERROR, L2TP_PLUGIN_UI_ERROR,
L2TP_PLUGIN_UI_ERROR_FILE_NOT_L2TP, L2TP_PLUGIN_UI_ERROR_FILE_NOT_L2TP,
_("unknown L2TP file extension")); _("unknown L2TP file extension. Allowed .conf or .cnf"));
goto out;
}
if (!g_file_get_contents (path, &contents, NULL, error))
return NULL; return NULL;
}
lines = g_strsplit_set (contents, "\r\n", 0); if (!strstr (path, "l2tp")) {
if (g_strv_length (lines) <= 1) {
g_set_error (error, g_set_error (error,
L2TP_PLUGIN_UI_ERROR, L2TP_PLUGIN_UI_ERROR,
L2TP_PLUGIN_UI_ERROR_FILE_NOT_READABLE, L2TP_PLUGIN_UI_ERROR_FILE_NOT_L2TP,
_("not a valid L2TP configuration file")); _("Filename doesn't contains 'l2tp' substring."));
goto out; return NULL;
} }
connection = do_import (path, lines, error); connection = do_import (path, error);
out:
if (lines)
g_strfreev (lines);
g_free (contents);
return connection; return connection;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment