Commit e6a5d0be authored by Tambet Ingo's avatar Tambet Ingo

2008-04-22 Tambet Ingo <tambet@gmail.com>

	Implement GKeyFile system settings plugin.
	Implement writing system settings (currently supported only by GKeyFile plugin).

	* system-settings/src/main.c:
	* system-settings/src/dbus-settings.c: Move the communication with plugins
	from main.c to dbus-settings.c. Makes it possible to talk to all registered
	plugins for adding/updating/removing connections.

	* system-settings/src/nm-system-config-interface.c
	(nm_system_config_interface_add_connection): Implement
	(nm_system_config_interface_update_connection): Implement.
	(nm_system_config_interface_remove_connection): Implement.

	* system-settings/plugins/keyfile/Makefile.am:
	* system-settings/plugins/keyfile/plugin.[ch]:
	* system-settings/plugins/keyfile/writer.[ch]:
	* system-settings/plugins/keyfile/reader.[ch]: Implement.

	* system-settings/plugins/Makefile.am: Add GKeyFile plugin.

	* configure.in: Generate GKeyFile Makefile.

	* libnm-glib/nm-settings.c (impl_exported_connection_get_id): Fix a memory
	corruption, need to duplicate the returned string.
	(impl_exported_connection_update): Implement.
	(impl_exported_connection_delete): Implement.

	* introspection/nm-settings-system.xml: Add "AddConnection" method.

	* introspection/nm-exported-connection.xml: Add "Update" and "Delete" methods.


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3587 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
parent bfa6b28b
2008-04-22 Tambet Ingo <tambet@gmail.com>
Implement GKeyFile system settings plugin.
Implement writing system settings (currently supported only by GKeyFile plugin).
* system-settings/src/main.c:
* system-settings/src/dbus-settings.c: Move the communication with plugins
from main.c to dbus-settings.c. Makes it possible to talk to all registered
plugins for adding/updating/removing connections.
* system-settings/src/nm-system-config-interface.c
(nm_system_config_interface_add_connection): Implement
(nm_system_config_interface_update_connection): Implement.
(nm_system_config_interface_remove_connection): Implement.
* system-settings/plugins/keyfile/Makefile.am:
* system-settings/plugins/keyfile/plugin.[ch]:
* system-settings/plugins/keyfile/writer.[ch]:
* system-settings/plugins/keyfile/reader.[ch]: Implement.
* system-settings/plugins/Makefile.am: Add GKeyFile plugin.
* configure.in: Generate GKeyFile Makefile.
* libnm-glib/nm-settings.c (impl_exported_connection_get_id): Fix a memory
corruption, need to duplicate the returned string.
(impl_exported_connection_update): Implement.
(impl_exported_connection_delete): Implement.
* introspection/nm-settings-system.xml: Add "AddConnection" method.
* introspection/nm-exported-connection.xml: Add "Update" and "Delete" methods.
2008-04-22 Dan Williams <dcbw@redhat.com>
Patch from Charles R. Anderson (cra@wpi.edu)
......
......@@ -308,6 +308,7 @@ system-settings/src/Makefile
system-settings/plugins/Makefile
system-settings/plugins/ifcfg-fedora/Makefile
system-settings/plugins/ifcfg-suse/Makefile
system-settings/plugins/keyfile/Makefile
test/Makefile
test/test-common/Makefile
initscript/Makefile
......
......@@ -18,6 +18,25 @@
</arg>
</method>
<method name="Update">
<tp:docstring>
Update the connection.
</tp:docstring>
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_exported_connection_update"/>
<arg name="properties" type="a{sa{sv}}" direction="in">
<tp:docstring>
New connection properties.
</tp:docstring>
</arg>
</method>
<method name="Delete">
<tp:docstring>
Delete the connection.
</tp:docstring>
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_exported_connection_delete"/>
</method>
<method name="GetSettings">
<tp:docstring>
Get the settings maps describing this object.
......
......@@ -6,6 +6,18 @@
Implemented by the system settings service to provide additional settings to NetworkManager.
</tp:docstring>
<method name="AddConnection">
<tp:docstring>
Add new connection.
</tp:docstring>
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_settings_add_connection"/>
<arg name="connection" type="a{sa{sv}}" direction="in">
<tp:docstring>
Connection properties.
</tp:docstring>
</arg>
</method>
<property name="UnmanagedDevices" type="ao" access="read">
<tp:docstring>
The list of HAL UDIs of devices that should not be managed by NetworkManager.
......
......@@ -107,6 +107,14 @@ static gboolean impl_exported_connection_get_id (NMExportedConnection *connectio
static gboolean impl_exported_connection_get_settings (NMExportedConnection *connection,
GHashTable **settings,
GError **error);
static gboolean impl_exported_connection_update (NMExportedConnection *connection,
GHashTable *new_settings,
GError *err);
static gboolean impl_exported_connection_delete (NMExportedConnection *connection,
GError *err);
static void impl_exported_connection_get_secrets (NMExportedConnection *connection,
const gchar *setting_name,
const gchar **hints,
......@@ -169,7 +177,7 @@ impl_exported_connection_get_id (NMExportedConnection *connection,
{
g_return_val_if_fail (NM_IS_EXPORTED_CONNECTION (connection), FALSE);
*id = (gchar *) nm_exported_connection_get_id (connection);
*id = g_strdup (nm_exported_connection_get_id (connection));
if (!*id) {
g_set_error (error, NM_SETTINGS_ERROR, 1,
"%s.%d - Could not get connection ID.",
......@@ -182,8 +190,8 @@ impl_exported_connection_get_id (NMExportedConnection *connection,
static gboolean
impl_exported_connection_get_settings (NMExportedConnection *connection,
GHashTable **settings,
GError **error)
GHashTable **settings,
GError **error)
{
NMExportedConnectionPrivate *priv;
......@@ -199,6 +207,32 @@ impl_exported_connection_get_settings (NMExportedConnection *connection,
return TRUE;
}
static gboolean
impl_exported_connection_update (NMExportedConnection *connection,
GHashTable *new_settings,
GError *err)
{
if (EXPORTED_CONNECTION_CLASS (connection)->update)
EXPORTED_CONNECTION_CLASS (connection)->update (connection, new_settings);
else
nm_connection_replace_settings (NM_EXPORTED_CONNECTION_GET_PRIVATE (connection)->wrapped, new_settings);
nm_exported_connection_signal_updated (connection, new_settings);
return TRUE;
}
static gboolean
impl_exported_connection_delete (NMExportedConnection *connection, GError *err)
{
if (EXPORTED_CONNECTION_CLASS (connection)->delete)
EXPORTED_CONNECTION_CLASS (connection)->delete (connection);
nm_exported_connection_signal_removed (connection);
return TRUE;
}
static void
impl_exported_connection_get_secrets (NMExportedConnection *connection,
const gchar *setting_name,
......
......@@ -37,6 +37,11 @@ typedef struct {
gboolean request_new,
DBusGMethodInvocation *context);
void (*update) (NMExportedConnection *connection,
GHashTable *new_settings);
void (*delete) (NMExportedConnection *connection);
/* signals */
void (* updated) (NMExportedConnection *connection, GHashTable *settings);
void (* removed) (NMExportedConnection *connection);
......
SUBDIRS=keyfile
if TARGET_REDHAT
SUBDIRS=ifcfg-fedora
SUBDIRS+=ifcfg-fedora
endif
if TARGET_SUSE
SUBDIRS=ifcfg-suse
SUBDIRS+=ifcfg-suse
endif
if TARGET_MANDRIVA
SUBDIRS=ifcfg-fedora
SUBDIRS+=ifcfg-fedora
endif
pkglib_LTLIBRARIES = libnm-settings-plugin-keyfile.la
libnm_settings_plugin_keyfile_la_SOURCES = \
plugin.c \
plugin.h \
reader.c \
reader.h \
writer.c \
writer.h
libnm_settings_plugin_keyfile_la_CPPFLAGS = \
$(GLIB_CFLAGS) \
$(GMODULE_CFLAGS) \
$(DBUS_CFLAGS) \
-DG_DISABLE_DEPRECATED \
-I${top_srcdir}/system-settings/src \
-I$(top_srcdir)/include \
-I$(top_srcdir)/libnm-util \
-DKEYFILE_DIR=\""$(sysconfdir)/NetworkManager/system_config"\"
libnm_settings_plugin_keyfile_la_LDFLAGS = -module -avoid-version
libnm_settings_plugin_keyfile_la_LIBADD = \
$(GLIB_LIBS) \
$(GMODULE_LIBS) \
$(GIO_LIBS) \
$(top_builddir)/libnm-util/libnm-util.la
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <gmodule.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <nm-connection.h>
#include <nm-setting.h>
#include <nm-setting-connection.h>
#include "plugin.h"
#include "nm-system-config-interface.h"
#include "reader.h"
#include "writer.h"
#define KEYFILE_PLUGIN_NAME "keyfile"
#define KEYFILE_PLUGIN_INFO "(c) 2007 - 2008 Red Hat, Inc. To report bugs please use the NetworkManager mailing list."
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
G_DEFINE_TYPE_EXTENDED (SCPluginKeyfile, sc_plugin_keyfile, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE,
system_config_interface_init))
#define SC_PLUGIN_KEYFILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SC_TYPE_PLUGIN_KEYFILE, SCPluginKeyfilePrivate))
typedef struct {
GHashTable *hash;
GFileMonitor *monitor;
guint monitor_id;
gboolean disposed;
} SCPluginKeyfilePrivate;
static NMConnection *
read_one_connection (NMSystemConfigInterface *config, const char *filename)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
char *full_path;
NMConnection *connection = NULL;
full_path = g_build_filename (KEYFILE_DIR, filename, NULL);
connection = connection_from_file (full_path);
if (connection)
g_hash_table_insert (priv->hash, g_strdup (filename), connection);
g_free (full_path);
return connection;
}
static void
read_connections (NMSystemConfigInterface *config)
{
GDir *dir;
GError *err = NULL;
dir = g_dir_open (KEYFILE_DIR, 0, &err);
if (dir) {
const char *item;
while ((item = g_dir_read_name (dir)))
read_one_connection (config, item);
g_dir_close (dir);
} else {
g_warning ("Can not read directory '%s': %s", KEYFILE_DIR, err->message);
g_error_free (err);
}
}
static void
delete_connection (NMSystemConfigInterface *config, NMConnection *connection)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
NMSettingConnection *s_con;
char *filename;
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
if (!s_con)
return;
filename = g_build_filename (KEYFILE_DIR, s_con->id, NULL);
if (g_hash_table_lookup (priv->hash, s_con->id)) {
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
/* Monitoring takes care of the rest */
g_unlink (filename);
else
g_warning ("File '%s' does not exist", filename);
}
g_free (filename);
}
/* Monitoring */
static void
dir_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
NMSystemConfigInterface *config = NM_SYSTEM_CONFIG_INTERFACE (user_data);
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
char *name;
NMConnection *connection;
name = g_file_get_basename (file);
connection = g_hash_table_lookup (priv->hash, name);
switch (event_type) {
case G_FILE_MONITOR_EVENT_DELETED:
if (connection) {
g_hash_table_remove (priv->hash, name);
g_signal_emit_by_name (config, "connection-removed", connection);
}
break;
case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
if (connection) {
/* Update */
char *full_path;
NMConnection *tmp;
full_path = g_file_get_path (file);
tmp = connection_from_file (full_path);
g_free (full_path);
if (tmp) {
GHashTable *settings;
settings = nm_connection_to_hash (tmp);
if (nm_connection_replace_settings (connection, settings))
g_signal_emit_by_name (config, "connection-updated", connection);
g_hash_table_destroy (settings);
g_object_unref (tmp);
}
} else {
/* New */
connection = read_one_connection (config, name);
if (connection)
g_signal_emit_by_name (config, "connection-added", connection);
}
break;
default:
break;
}
g_free (name);
}
static void
setup_monitoring (NMSystemConfigInterface *config)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
GFile *file;
GFileMonitor *monitor;
priv->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
file = g_file_new_for_path (KEYFILE_DIR);
monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
g_object_unref (file);
if (monitor) {
priv->monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (dir_changed), config);
priv->monitor = monitor;
}
}
static void
hash_to_slist (gpointer key, gpointer value, gpointer user_data)
{
GSList **list = (GSList **) user_data;
*list = g_slist_prepend (*list, value);
}
/* Plugin */
static GSList *
get_connections (NMSystemConfigInterface *config)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
GSList *connections = NULL;
if (!priv->hash) {
setup_monitoring (config);
read_connections (config);
}
g_hash_table_foreach (priv->hash, hash_to_slist, &connections);
return connections;
}
static void
add_connection (NMSystemConfigInterface *config, NMConnection *connection)
{
write_connection (connection);
}
static void
update_connection (NMSystemConfigInterface *config, NMConnection *connection)
{
write_connection (connection);
}
static void
remove_connection (NMSystemConfigInterface *config, NMConnection *connection)
{
delete_connection (config, connection);
}
/* GObject */
static void
sc_plugin_keyfile_init (SCPluginKeyfile *plugin)
{
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
g_value_set_string (value, KEYFILE_PLUGIN_NAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
g_value_set_string (value, KEYFILE_PLUGIN_INFO);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
dispose (GObject *object)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (object);
if (priv->disposed)
return;
priv->disposed = TRUE;
if (priv->monitor) {
if (priv->monitor_id)
g_signal_handler_disconnect (priv->monitor, priv->monitor_id);
g_file_monitor_cancel (priv->monitor);
g_object_unref (priv->monitor);
}
g_hash_table_destroy (priv->hash);
G_OBJECT_CLASS (sc_plugin_keyfile_parent_class)->dispose (object);
}
static void
sc_plugin_keyfile_class_init (SCPluginKeyfileClass *req_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (req_class);
g_type_class_add_private (req_class, sizeof (SCPluginKeyfilePrivate));
object_class->dispose = dispose;
object_class->get_property = get_property;
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
NM_SYSTEM_CONFIG_INTERFACE_NAME);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
NM_SYSTEM_CONFIG_INTERFACE_INFO);
}
static void
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
{
/* interface implementation */
system_config_interface_class->get_connections = get_connections;
system_config_interface_class->add_connection = add_connection;
system_config_interface_class->update_connection = update_connection;
system_config_interface_class->remove_connection = remove_connection;
}
G_MODULE_EXPORT GObject *
nm_system_config_factory (void)
{
static SCPluginKeyfile *singleton = NULL;
if (!singleton)
singleton = SC_PLUGIN_KEYFILE (g_object_new (SC_TYPE_PLUGIN_KEYFILE, NULL));
else
g_object_ref (singleton);
return G_OBJECT (singleton);
}
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
#ifndef _PLUGIN_H_
#define _PLUGIN_H_
#include <glib-object.h>
#define SC_TYPE_PLUGIN_KEYFILE (sc_plugin_keyfile_get_type ())
#define SC_PLUGIN_KEYFILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SC_TYPE_PLUGIN_KEYFILE, SCPluginKeyfile))
#define SC_PLUGIN_KEYFILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SC_TYPE_PLUGIN_KEYFILE, SCPluginKeyfileClass))
#define SC_IS_PLUGIN_KEYFILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SC_TYPE_PLUGIN_KEYFILE))
#define SC_IS_PLUGIN_KEYFILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), SC_TYPE_PLUGIN_KEYFILE))
#define SC_PLUGIN_KEYFILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SC_TYPE_PLUGIN_KEYFILE, SCPluginKeyfileClass))
typedef struct {
GObject parent;
} SCPluginKeyfile;
typedef struct {
GObjectClass parent;
} SCPluginKeyfileClass;
GType sc_plugin_keyfile_get_type (void);
GQuark keyfile_plugin_error_quark (void);
#endif /* _PLUGIN_H_ */
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dbus/dbus-glib.h>
#include <nm-setting.h>
#include "reader.h"
#define DBUS_TYPE_G_ARRAY_OF_UINT (dbus_g_type_get_collection ("GArray", G_TYPE_UINT))
#define DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_ARRAY_OF_UINT))
static void
read_one_setting_value (NMSetting *setting,
const char *key,
const GValue *value,
gboolean secret,
gpointer user_data)
{
GKeyFile *file = (GKeyFile *) user_data;
GType type;
GError *err = NULL;
if (!g_key_file_has_key (file, setting->name, key, &err)) {
if (err) {
g_warning ("Error loading setting '%s' value: %s", setting->name, err->message);
g_error_free (err);
}
return;
}
type = G_VALUE_TYPE (value);
if (type == G_TYPE_STRING) {
char *str_val;
str_val = g_key_file_get_string (file, setting->name, key, NULL);
g_object_set (setting, key, str_val, NULL);
g_free (str_val);
} else if (type == G_TYPE_UINT) {
int int_val;
int_val = g_key_file_get_integer (file, setting->name, key, NULL);
if (int_val < 0)
g_warning ("Casting negative value (%i) to uint", int_val);
g_object_set (setting, key, int_val, NULL);
} else if (type == G_TYPE_INT) {
int int_val;
int_val = g_key_file_get_integer (file, setting->name, key, NULL);
g_object_set (setting, key, int_val, NULL);
} else if (type == G_TYPE_BOOLEAN) {
gboolean bool_val;
bool_val = g_key_file_get_boolean (file, setting->name, key, NULL);
g_object_set (setting, key, bool_val, NULL);
} else if (type == G_TYPE_CHAR) {
int int_val;
int_val = g_key_file_get_integer (file, setting->name, key, NULL);
if (int_val < G_MININT8 || int_val > G_MAXINT8)
g_warning ("Casting value (%i) to char", int_val);
g_object_set (setting, key, int_val, NULL);
} else if (type == G_TYPE_UINT64) {
char *tmp_str;
guint64 uint_val;
tmp_str = g_key_file_get_value (file, setting->name, key, NULL);
uint_val = g_ascii_strtoull (tmp_str, NULL, 10);
g_object_set (setting, key, uint_val, NULL);
} else if (type == DBUS_TYPE_G_UCHAR_ARRAY) {
gint *tmp;
GByteArray *array;
gsize length;
int i;
tmp = g_key_file_get_integer_list (file, setting->name, key, &length, NULL);
array = g_byte_array_sized_new (length);
for (i = 0; i < length; i++) {
int val = tmp[i];
unsigned char v = (unsigned char) (val & 0xFF);
if (val < 0 || val > 255)
g_warning ("Value out of range for a byte value");
else
g_byte_array_append (array, (const unsigned char *) &v, sizeof (v));
}
g_object_set (setting, key, array, NULL);
g_byte_array_free (array, TRUE);
} else if (type == dbus_g_type_get_collection ("GSList", G_TYPE_STRING)) {
gchar **sa;
gsize length;
int i;
GSList *list = NULL;
sa = g_key_file_get_string_list (file, setting->name, key, &length, NULL);
for (i = 0; i < length; i++)
list = g_slist_prepend (list, sa[i]);
list = g_slist_reverse (list);
g_object_set (setting, key, list, NULL);
g_slist_free (list);
g_strfreev (sa);
} else if (type == dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)) {
/* FIXME */
g_warning ("Implement me");
} else if (type == DBUS_TYPE_G_UINT_ARRAY) {
gint *tmp;
GArray *array;
gsize length;
int i;
tmp = g_key_file_get_integer_list (file, setting->name, key, &length, NULL);
array = g_array_sized_new (FALSE, FALSE, sizeof (guint32), length);
for (i = 0; i < length; i++)
g_array_append_val (array, tmp[i]);
g_object_set (setting, key, array, NULL);
g_array_free (array, TRUE);
} else if (type == DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT) {
/* FIXME */
g_warning ("Implement me");
} else {
g_warning ("Unhandled setting property type (read): '%s/%s' : '%s'",
setting->name, key, G_VALUE_TYPE_NAME (value));
}
}
static NMSetting *
read_setting (GKeyFile *file, const char *name)
{
NMSetting *setting;
setting = nm_connection_create_setting (name);
if (setting) {
nm_setting_enumerate_values (setting, read_one_setting_value, file);
} else
g_warning ("Invalid setting name '%s'", name);
return setting;
}
NMConnection *
connection_from_file (const char *filename)
{
GKeyFile *key_file;
struct stat statbuf;
gboolean bad_owner, bad_permissions;
NMConnection *connection = NULL;
GError *err = NULL;
if (stat (filename, &statbuf) != 0 || !S_ISREG (statbuf.st_mode))
return NULL;
bad_owner = getuid () != statbuf.st_uid;
bad_permissions = statbuf.st_mode & 0077;