NetworkManager/system-settings/plugins/keyfile/nm-keyfile-connection.c

184 lines
5.8 KiB
C
Raw Normal View History

/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager system settings service - keyfile plugin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2008 Novell, Inc.
* Copyright (C) 2008 - 2011 Red Hat, Inc.
*/
#include <string.h>
#include <glib/gstdio.h>
#include <NetworkManager.h>
#include <nm-setting-connection.h>
#include <nm-utils.h>
#include "nm-system-config-interface.h"
#include "nm-dbus-glib-types.h"
#include "nm-keyfile-connection.h"
#include "reader.h"
#include "writer.h"
#include "common.h"
G_DEFINE_TYPE (NMKeyfileConnection, nm_keyfile_connection, NM_TYPE_SETTINGS_CONNECTION)
#define NM_KEYFILE_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_KEYFILE_CONNECTION, NMKeyfileConnectionPrivate))
typedef struct {
char *path;
} NMKeyfileConnectionPrivate;
NMKeyfileConnection *
nm_keyfile_connection_new (const char *full_path,
NMConnection *source,
GError **error)
{
GObject *object;
NMKeyfileConnectionPrivate *priv;
NMSettingConnection *s_con;
NMConnection *tmp;
g_return_val_if_fail (full_path != NULL, NULL);
/* If we're given a connection already, prefer that instead of re-reading */
if (source)
tmp = g_object_ref (source);
else {
tmp = nm_keyfile_plugin_connection_from_file (full_path, error);
if (!tmp)
return NULL;
}
object = (GObject *) g_object_new (NM_TYPE_KEYFILE_CONNECTION, NULL);
if (!object)
goto out;
priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (object);
priv->path = g_strdup (full_path);
/* Update our settings with what was read from the file */
if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (object), tmp, error)) {
g_object_unref (object);
object = NULL;
goto out;
}
/* if for some reason the connection didn't have a UUID, add one */
s_con = (NMSettingConnection *) nm_connection_get_setting (NM_CONNECTION (object), NM_TYPE_SETTING_CONNECTION);
if (s_con && !nm_setting_connection_get_uuid (s_con)) {
GError *write_error = NULL;
char *uuid;
uuid = nm_utils_uuid_generate ();
g_object_set (s_con, NM_SETTING_CONNECTION_UUID, uuid, NULL);
g_free (uuid);
if (!write_connection (NM_CONNECTION (object), KEYFILE_DIR, 0, 0, NULL, &write_error)) {
PLUGIN_WARN (KEYFILE_PLUGIN_NAME,
"Couldn't update connection %s with a UUID: (%d) %s",
nm_setting_connection_get_id (s_con),
write_error ? write_error->code : -1,
(write_error && write_error->message) ? write_error->message : "(unknown)");
g_propagate_error (error, write_error);
}
}
out:
g_object_unref (tmp);
return (NMKeyfileConnection *) object;
}
const char *
nm_keyfile_connection_get_path (NMKeyfileConnection *self)
{
g_return_val_if_fail (NM_IS_KEYFILE_CONNECTION (self), NULL);
return NM_KEYFILE_CONNECTION_GET_PRIVATE (self)->path;
}
static void
commit_changes (NMSettingsConnection *connection,
NMSettingsConnectionCommitFunc callback,
gpointer user_data)
{
NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (connection);
char *path = NULL;
GError *error = NULL;
2008-05-08 Tambet Ingo <tambet@gmail.com> Use PolicyKit to authorize the system settings' AddConnection method and the system settings connections' Update and Delete methods. * libnm-glib/nm-settings.c (impl_exported_connection_update) (impl_exported_connection_delete, nm_exported_connection_update) (nm_exported_connection_delete): Return boolean and fill GError to notify the callers of the reasons why it might have failed. * libnm-glib/nm-dbus-settings-system.c (nm_dbus_settings_system_add_connection): Return the error from dbus call so that the callers can see why it failed. * libnm-glib/nm-dbus-connection.c (update, delete): Update the signatures. * system-settings/src/nm-polkit-helpers.[ch]: Implement. * system-settings/src/nm-sysconfig-connection.[ch]: Implement. New abstract base class that checks PolicyKit permissions. * system-settings/src/dbus-settings.c: (impl_settings_add_connection): Check the policy before carring out the request. * system-settings/plugins/keyfile/nm-keyfile-connection.c: Inherit from NMSysconfigConnection, check the policies before allowing updating or removing. * system-settings/plugins/ifcfg-suse/nm-suse-connection.c: Inherit from NMSysconfigConnection. * introspection/nm-exported-connection.xml: Annotate "Update" and "Delete" methods with async flag so that the implementations can get access to DBusGMethodInvocation. * system-settings/src/dbus-settings.c (settings_add_connection_check_privileges): Implement. (impl_settings_add_connection): Check the privileges before adding a new connection. Improve error reporting. * introspection/nm-settings-system.xml: Make the 'AddConnection' method async so that the implementation can access DBusGMethodInvocation. * configure.in: Check for PolicyKit. * policy/org.freedesktop.network-manager-settings.system.policy: New file. * policy/Makefile.am: Install the policy file. * configure.in: Add 'policy' subdir. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3646 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2008-05-09 06:33:30 +00:00
if (!write_connection (NM_CONNECTION (connection), KEYFILE_DIR, 0, 0, &path, &error)) {
callback (connection, error, user_data);
g_clear_error (&error);
return;
}
if (g_strcmp0 (priv->path, path)) {
/* Update the filename if it changed */
g_free (priv->path);
priv->path = path;
} else
g_free (path);
2008-05-08 Tambet Ingo <tambet@gmail.com> Use PolicyKit to authorize the system settings' AddConnection method and the system settings connections' Update and Delete methods. * libnm-glib/nm-settings.c (impl_exported_connection_update) (impl_exported_connection_delete, nm_exported_connection_update) (nm_exported_connection_delete): Return boolean and fill GError to notify the callers of the reasons why it might have failed. * libnm-glib/nm-dbus-settings-system.c (nm_dbus_settings_system_add_connection): Return the error from dbus call so that the callers can see why it failed. * libnm-glib/nm-dbus-connection.c (update, delete): Update the signatures. * system-settings/src/nm-polkit-helpers.[ch]: Implement. * system-settings/src/nm-sysconfig-connection.[ch]: Implement. New abstract base class that checks PolicyKit permissions. * system-settings/src/dbus-settings.c: (impl_settings_add_connection): Check the policy before carring out the request. * system-settings/plugins/keyfile/nm-keyfile-connection.c: Inherit from NMSysconfigConnection, check the policies before allowing updating or removing. * system-settings/plugins/ifcfg-suse/nm-suse-connection.c: Inherit from NMSysconfigConnection. * introspection/nm-exported-connection.xml: Annotate "Update" and "Delete" methods with async flag so that the implementations can get access to DBusGMethodInvocation. * system-settings/src/dbus-settings.c (settings_add_connection_check_privileges): Implement. (impl_settings_add_connection): Check the privileges before adding a new connection. Improve error reporting. * introspection/nm-settings-system.xml: Make the 'AddConnection' method async so that the implementation can access DBusGMethodInvocation. * configure.in: Check for PolicyKit. * policy/org.freedesktop.network-manager-settings.system.policy: New file. * policy/Makefile.am: Install the policy file. * configure.in: Add 'policy' subdir. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3646 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2008-05-09 06:33:30 +00:00
NM_SETTINGS_CONNECTION_CLASS (nm_keyfile_connection_parent_class)->commit_changes (connection,
callback,
user_data);
}
static void
do_delete (NMSettingsConnection *connection,
NMSettingsConnectionDeleteFunc callback,
gpointer user_data)
{
NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (connection);
2008-05-08 Tambet Ingo <tambet@gmail.com> Use PolicyKit to authorize the system settings' AddConnection method and the system settings connections' Update and Delete methods. * libnm-glib/nm-settings.c (impl_exported_connection_update) (impl_exported_connection_delete, nm_exported_connection_update) (nm_exported_connection_delete): Return boolean and fill GError to notify the callers of the reasons why it might have failed. * libnm-glib/nm-dbus-settings-system.c (nm_dbus_settings_system_add_connection): Return the error from dbus call so that the callers can see why it failed. * libnm-glib/nm-dbus-connection.c (update, delete): Update the signatures. * system-settings/src/nm-polkit-helpers.[ch]: Implement. * system-settings/src/nm-sysconfig-connection.[ch]: Implement. New abstract base class that checks PolicyKit permissions. * system-settings/src/dbus-settings.c: (impl_settings_add_connection): Check the policy before carring out the request. * system-settings/plugins/keyfile/nm-keyfile-connection.c: Inherit from NMSysconfigConnection, check the policies before allowing updating or removing. * system-settings/plugins/ifcfg-suse/nm-suse-connection.c: Inherit from NMSysconfigConnection. * introspection/nm-exported-connection.xml: Annotate "Update" and "Delete" methods with async flag so that the implementations can get access to DBusGMethodInvocation. * system-settings/src/dbus-settings.c (settings_add_connection_check_privileges): Implement. (impl_settings_add_connection): Check the privileges before adding a new connection. Improve error reporting. * introspection/nm-settings-system.xml: Make the 'AddConnection' method async so that the implementation can access DBusGMethodInvocation. * configure.in: Check for PolicyKit. * policy/org.freedesktop.network-manager-settings.system.policy: New file. * policy/Makefile.am: Install the policy file. * configure.in: Add 'policy' subdir. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3646 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2008-05-09 06:33:30 +00:00
g_unlink (priv->path);
2008-05-08 Tambet Ingo <tambet@gmail.com> Use PolicyKit to authorize the system settings' AddConnection method and the system settings connections' Update and Delete methods. * libnm-glib/nm-settings.c (impl_exported_connection_update) (impl_exported_connection_delete, nm_exported_connection_update) (nm_exported_connection_delete): Return boolean and fill GError to notify the callers of the reasons why it might have failed. * libnm-glib/nm-dbus-settings-system.c (nm_dbus_settings_system_add_connection): Return the error from dbus call so that the callers can see why it failed. * libnm-glib/nm-dbus-connection.c (update, delete): Update the signatures. * system-settings/src/nm-polkit-helpers.[ch]: Implement. * system-settings/src/nm-sysconfig-connection.[ch]: Implement. New abstract base class that checks PolicyKit permissions. * system-settings/src/dbus-settings.c: (impl_settings_add_connection): Check the policy before carring out the request. * system-settings/plugins/keyfile/nm-keyfile-connection.c: Inherit from NMSysconfigConnection, check the policies before allowing updating or removing. * system-settings/plugins/ifcfg-suse/nm-suse-connection.c: Inherit from NMSysconfigConnection. * introspection/nm-exported-connection.xml: Annotate "Update" and "Delete" methods with async flag so that the implementations can get access to DBusGMethodInvocation. * system-settings/src/dbus-settings.c (settings_add_connection_check_privileges): Implement. (impl_settings_add_connection): Check the privileges before adding a new connection. Improve error reporting. * introspection/nm-settings-system.xml: Make the 'AddConnection' method async so that the implementation can access DBusGMethodInvocation. * configure.in: Check for PolicyKit. * policy/org.freedesktop.network-manager-settings.system.policy: New file. * policy/Makefile.am: Install the policy file. * configure.in: Add 'policy' subdir. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3646 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2008-05-09 06:33:30 +00:00
NM_SETTINGS_CONNECTION_CLASS (nm_keyfile_connection_parent_class)->delete (connection,
callback,
user_data);
}
/* GObject */
static void
nm_keyfile_connection_init (NMKeyfileConnection *connection)
{
}
static void
finalize (GObject *object)
{
NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (object);
nm_connection_clear_secrets (NM_CONNECTION (object));
g_free (priv->path);
G_OBJECT_CLASS (nm_keyfile_connection_parent_class)->finalize (object);
}
static void
nm_keyfile_connection_class_init (NMKeyfileConnectionClass *keyfile_connection_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (keyfile_connection_class);
NMSettingsConnectionClass *settings_class = NM_SETTINGS_CONNECTION_CLASS (keyfile_connection_class);
g_type_class_add_private (keyfile_connection_class, sizeof (NMKeyfileConnectionPrivate));
/* Virtual methods */
object_class->finalize = finalize;
settings_class->commit_changes = commit_changes;
settings_class->delete = do_delete;
}