2008-08-27 03:09:14 +00:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
2008-11-03 16:05:11 +00:00
|
|
|
/* 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.
|
2010-09-16 15:43:22 -05:00
|
|
|
* Copyright (C) 2008 - 2010 Red Hat, Inc.
|
2008-11-03 16:05:11 +00:00
|
|
|
*/
|
2008-05-07 09:48:12 +00:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
#include <NetworkManager.h>
|
2008-08-27 02:57:21 +00:00
|
|
|
#include <nm-setting-connection.h>
|
|
|
|
|
#include <nm-utils.h>
|
|
|
|
|
|
2010-09-16 15:43:22 -05:00
|
|
|
#include "nm-system-config-interface.h"
|
2008-09-04 14:32:14 +00:00
|
|
|
#include "nm-dbus-glib-types.h"
|
2008-05-07 09:48:12 +00:00
|
|
|
#include "nm-keyfile-connection.h"
|
|
|
|
|
#include "reader.h"
|
|
|
|
|
#include "writer.h"
|
2010-09-16 17:39:06 -05:00
|
|
|
#include "common.h"
|
2008-05-07 09:48:12 +00:00
|
|
|
|
2010-08-05 18:25:15 -04:00
|
|
|
G_DEFINE_TYPE (NMKeyfileConnection, nm_keyfile_connection, NM_TYPE_SYSCONFIG_CONNECTION)
|
2008-05-07 09:48:12 +00:00
|
|
|
|
|
|
|
|
#define NM_KEYFILE_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_KEYFILE_CONNECTION, NMKeyfileConnectionPrivate))
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2010-10-29 14:34:33 -05:00
|
|
|
char *path;
|
2008-05-07 09:48:12 +00:00
|
|
|
} NMKeyfileConnectionPrivate;
|
|
|
|
|
|
|
|
|
|
NMKeyfileConnection *
|
2010-10-29 14:34:33 -05:00
|
|
|
nm_keyfile_connection_new (const char *full_path,
|
|
|
|
|
NMConnection *source,
|
|
|
|
|
GError **error)
|
2008-05-07 09:48:12 +00:00
|
|
|
{
|
2010-09-16 15:43:22 -05:00
|
|
|
GObject *object;
|
|
|
|
|
NMKeyfileConnectionPrivate *priv;
|
|
|
|
|
NMSettingConnection *s_con;
|
|
|
|
|
NMConnection *tmp;
|
|
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
g_return_val_if_fail (full_path != NULL, NULL);
|
2008-05-07 09:48:12 +00:00
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
/* If we're given a connection already, prefer that instead of re-reading */
|
|
|
|
|
if (source)
|
|
|
|
|
tmp = g_object_ref (source);
|
|
|
|
|
else {
|
|
|
|
|
tmp = connection_from_file (full_path, error);
|
|
|
|
|
if (!tmp)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-09-16 15:43:22 -05:00
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
object = (GObject *) g_object_new (NM_TYPE_KEYFILE_CONNECTION, NULL);
|
2010-09-16 15:43:22 -05:00
|
|
|
if (!object) {
|
|
|
|
|
g_object_unref (tmp);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (object);
|
2010-10-29 14:34:33 -05:00
|
|
|
priv->path = g_strdup (full_path);
|
2010-09-16 15:43:22 -05:00
|
|
|
|
|
|
|
|
/* Update our settings with what was read from the file */
|
2010-09-16 17:10:49 -05:00
|
|
|
nm_sysconfig_connection_replace_settings (NM_SYSCONFIG_CONNECTION (object), tmp, NULL);
|
2010-09-16 15:43:22 -05:00
|
|
|
g_object_unref (tmp);
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NM_KEYFILE_CONNECTION (object);
|
2008-05-07 09:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *
|
2010-10-29 14:34:33 -05:00
|
|
|
nm_keyfile_connection_get_path (NMKeyfileConnection *self)
|
2008-05-07 09:48:12 +00:00
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (NM_IS_KEYFILE_CONNECTION (self), NULL);
|
|
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
return NM_KEYFILE_CONNECTION_GET_PRIVATE (self)->path;
|
2008-05-07 09:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-05 18:25:15 -04:00
|
|
|
static void
|
|
|
|
|
commit_changes (NMSysconfigConnection *connection,
|
|
|
|
|
NMSysconfigConnectionCommitFunc callback,
|
|
|
|
|
gpointer user_data)
|
2008-05-07 09:48:12 +00:00
|
|
|
{
|
2009-07-23 09:20:52 -04:00
|
|
|
NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (connection);
|
2010-10-29 14:34:33 -05:00
|
|
|
char *path = NULL;
|
2009-07-23 09:20:52 -04:00
|
|
|
GError *error = NULL;
|
2008-05-09 06:33:30 +00:00
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
if (!write_connection (NM_CONNECTION (connection), KEYFILE_DIR, 0, 0, &path, &error)) {
|
2010-07-28 02:28:45 -07:00
|
|
|
callback (connection, error, user_data);
|
|
|
|
|
g_clear_error (&error);
|
2010-08-05 18:25:15 -04:00
|
|
|
return;
|
2010-07-28 02:28:45 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
if (g_strcmp0 (priv->path, path)) {
|
2009-07-23 09:20:52 -04:00
|
|
|
/* Update the filename if it changed */
|
2010-10-29 14:34:33 -05:00
|
|
|
g_free (priv->path);
|
|
|
|
|
priv->path = path;
|
2010-07-28 02:28:45 -07:00
|
|
|
} else
|
2010-10-29 14:34:33 -05:00
|
|
|
g_free (path);
|
2008-05-09 06:33:30 +00:00
|
|
|
|
2010-08-05 18:25:15 -04:00
|
|
|
NM_SYSCONFIG_CONNECTION_CLASS (nm_keyfile_connection_parent_class)->commit_changes (connection,
|
|
|
|
|
callback,
|
|
|
|
|
user_data);
|
2008-05-07 09:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-05 18:25:15 -04:00
|
|
|
static void
|
|
|
|
|
do_delete (NMSysconfigConnection *connection,
|
|
|
|
|
NMSysconfigConnectionDeleteFunc callback,
|
|
|
|
|
gpointer user_data)
|
2008-05-07 09:48:12 +00:00
|
|
|
{
|
2009-07-23 09:20:52 -04:00
|
|
|
NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (connection);
|
2008-05-09 06:33:30 +00:00
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
g_unlink (priv->path);
|
2008-05-09 06:33:30 +00:00
|
|
|
|
2010-08-05 18:25:15 -04:00
|
|
|
NM_SYSCONFIG_CONNECTION_CLASS (nm_keyfile_connection_parent_class)->delete (connection,
|
|
|
|
|
callback,
|
|
|
|
|
user_data);
|
2008-05-07 09:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GObject */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_keyfile_connection_init (NMKeyfileConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
finalize (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
NMKeyfileConnectionPrivate *priv = NM_KEYFILE_CONNECTION_GET_PRIVATE (object);
|
|
|
|
|
|
2009-07-23 09:20:52 -04:00
|
|
|
nm_connection_clear_secrets (NM_CONNECTION (object));
|
|
|
|
|
|
2010-10-29 14:34:33 -05:00
|
|
|
g_free (priv->path);
|
2008-05-07 09:48:12 +00:00
|
|
|
|
|
|
|
|
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);
|
2010-08-05 18:25:15 -04:00
|
|
|
NMSysconfigConnectionClass *sysconfig_class = NM_SYSCONFIG_CONNECTION_CLASS (keyfile_connection_class);
|
2008-05-07 09:48:12 +00:00
|
|
|
|
|
|
|
|
g_type_class_add_private (keyfile_connection_class, sizeof (NMKeyfileConnectionPrivate));
|
|
|
|
|
|
|
|
|
|
/* Virtual methods */
|
|
|
|
|
object_class->finalize = finalize;
|
2010-08-05 18:25:15 -04:00
|
|
|
sysconfig_class->commit_changes = commit_changes;
|
|
|
|
|
sysconfig_class->delete = do_delete;
|
2008-05-07 09:48:12 +00:00
|
|
|
}
|