ifcfg-rh: begin adding write support

This commit is contained in:
Dan Williams 2009-03-25 14:45:31 -04:00
parent 865163d041
commit 1974b257e0
8 changed files with 224 additions and 17 deletions

View file

@ -9,6 +9,8 @@ libifcfg_rh_io_la_SOURCES = \
shvar.h \
reader.c \
reader.h \
writer.c \
writer.h \
sha1.c \
sha1.h \
errors.c \

View file

@ -28,6 +28,8 @@
#define ORIG_TAG ".orig"
#define REJ_TAG ".rej"
#define IFCFG_DIR SYSCONFDIR"/sysconfig/network-scripts"
#define IFCFG_PLUGIN_NAME "ifcfg-rh"
#define IFCFG_PLUGIN_INFO "(c) 2007 - 2008 Red Hat, Inc. To report bugs please use the NetworkManager mailing list."

View file

@ -15,7 +15,7 @@
* 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 Red Hat, Inc.
* Copyright (C) 2008 - 2009 Red Hat, Inc.
*/
#include <string.h>
@ -38,6 +38,7 @@
#include "nm-ifcfg-connection.h"
#include "nm-system-config-hal-manager.h"
#include "reader.h"
#include "writer.h"
#include "nm-inotify-helper.h"
G_DEFINE_TYPE (NMIfcfgConnection, nm_ifcfg_connection, NM_TYPE_SYSCONFIG_CONNECTION)
@ -319,8 +320,24 @@ nm_ifcfg_connection_get_unmanaged (NMIfcfgConnection *self)
static gboolean
update (NMExportedConnection *exported, GHashTable *new_settings, GError **error)
{
// write_connection (NM_IFCFG_CONNECTION (exported));
return TRUE;
NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (exported);
gboolean success;
NMConnection *connection;
success = NM_EXPORTED_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->update (exported, new_settings, error);
if (success) {
connection = nm_exported_connection_get_connection (exported);
success = nm_connection_replace_settings (connection, new_settings, error);
if (success) {
success = write_connection (connection,
IFCFG_DIR,
priv->filename,
priv->keyfile,
error);
}
}
return success;
}
static gboolean

View file

@ -48,8 +48,7 @@
#include "nm-ifcfg-connection.h"
#include "nm-inotify-helper.h"
#include "shvar.h"
#define IFCFG_DIR SYSCONFDIR"/sysconfig/network-scripts/"
#include "writer.h"
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
@ -435,7 +434,7 @@ setup_ifcfg_monitoring (SCPluginIfcfg *plugin)
priv->connections = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
file = g_file_new_for_path (IFCFG_DIR);
file = g_file_new_for_path (IFCFG_DIR "/");
monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
g_object_unref (file);
@ -472,6 +471,14 @@ get_connections (NMSystemConfigInterface *config)
return list;
}
static gboolean
add_connection (NMSystemConfigInterface *config,
NMConnection *connection,
GError **error)
{
return write_connection (connection, IFCFG_DIR, NULL, NULL, error);
}
#define SC_NETWORK_FILE SYSCONFDIR"/sysconfig/network"
static char *
@ -697,6 +704,7 @@ system_config_interface_init (NMSystemConfigInterface *system_config_interface_c
{
/* interface implementation */
system_config_interface_class->get_connections = get_connections;
system_config_interface_class->add_connection = add_connection;
system_config_interface_class->get_unmanaged_devices = get_unmanaged_devices;
system_config_interface_class->init = init;
}

View file

@ -39,6 +39,7 @@
#undef __user
#include <glib.h>
#include <glib/gi18n.h>
#include <nm-connection.h>
#include <NetworkManager.h>
#include <nm-setting-connection.h>
@ -125,6 +126,7 @@ make_connection_setting (const char *file,
NMSettingConnection *s_con;
char *ifcfg_name = NULL;
char *new_id = NULL, *uuid = NULL, *value;
char *ifcfg_id;
ifcfg_name = get_ifcfg_name (file);
if (!ifcfg_name)
@ -132,22 +134,32 @@ make_connection_setting (const char *file,
s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
if (suggested) {
/* For cosmetic reasons, if the suggested name is the same as
* the ifcfg files name, don't use it.
*/
if (strcmp (ifcfg_name, suggested)) {
new_id = g_strdup_printf ("System %s (%s)", suggested, ifcfg_name);
/* Try the ifcfg file's internally defined name if available */
ifcfg_id = svGetValue (ifcfg, "NAME", FALSE);
if (ifcfg_id && strlen (ifcfg_id))
g_object_set (s_con, NM_SETTING_CONNECTION_ID, ifcfg_id, NULL);
if (!nm_setting_connection_get_id (s_con)) {
if (suggested) {
/* For cosmetic reasons, if the suggested name is the same as
* the ifcfg files name, don't use it. Mainly for wifi so that
* the SSID is shown in the connection ID instead of just "wlan0".
*/
if (strcmp (ifcfg_name, suggested)) {
new_id = g_strdup_printf ("%s %s (%s)", reader_get_prefix (), suggested, ifcfg_name);
g_object_set (s_con, NM_SETTING_CONNECTION_ID, new_id, NULL);
}
}
/* Use the ifcfg file's name as a last resort */
if (!nm_setting_connection_get_id (s_con)) {
new_id = g_strdup_printf ("%s %s", reader_get_prefix (), ifcfg_name);
g_object_set (s_con, NM_SETTING_CONNECTION_ID, new_id, NULL);
}
}
if (!nm_setting_connection_get_id (s_con)) {
new_id = g_strdup_printf ("System %s", ifcfg_name);
g_object_set (s_con, NM_SETTING_CONNECTION_ID, new_id, NULL);
}
g_free (new_id);
g_free (ifcfg_id);
/* Try for a UUID key before falling back to hashing the file name */
uuid = svGetValue (ifcfg, "UUID", FALSE);
@ -1896,4 +1908,9 @@ done:
return connection;
}
const char *
reader_get_prefix (void)
{
return _("System");
}

View file

@ -35,4 +35,6 @@ NMConnection *connection_from_file (const char *filename,
GError **error,
gboolean *ignore_error);
const char *reader_get_prefix (void);
#endif /* __READER_H__ */

View file

@ -0,0 +1,125 @@
/* -*- 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) 2009 Red Hat, Inc.
*/
#include <ctype.h>
#include <string.h>
#include <nm-setting-connection.h>
#include <nm-setting-wired.h>
#include <nm-setting-wireless.h>
#include "common.h"
#include "shvar.h"
#include "writer.h"
static void
write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg)
{
char *tmp;
svSetValue (ifcfg, "NAME", nm_setting_connection_get_id (s_con));
svSetValue (ifcfg, "UUID", nm_setting_connection_get_uuid (s_con));
svSetValue (ifcfg, "ONBOOT",
nm_setting_connection_get_autoconnect (s_con) ? "yes" : "no");
tmp = g_strdup_printf ("%llu", nm_setting_connection_get_timestamp (s_con));
svSetValue (ifcfg, "LAST_CONNECTION", tmp);
g_free (tmp);
}
static char *
escape_id (const char *id)
{
char *escaped = g_strdup (id);
char *p = escaped;
/* Escape random stuff */
while (*p) {
if (*p == ' ')
*p = '_';
else if (*p == '/')
*p = '-';
else if (*p == '\\')
*p = '-';
}
return escaped;
}
gboolean
write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
const char *keyfile,
GError **error)
{
NMSettingConnection *s_con;
gboolean success = FALSE;
shvarFile *ifcfg = NULL;
char *ifcfg_name = NULL;
const char *type;
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
if (!s_con) {
g_set_error (error, ifcfg_plugin_error_quark (), 0,
"Missing '%s' setting", NM_SETTING_CONNECTION_SETTING_NAME);
return FALSE;
}
if (filename) {
/* For existing connections, 'filename' should be full path to ifcfg file */
ifcfg = svNewFile (filename);
ifcfg_name = g_strdup (filename);
} else {
char *escaped;
escaped = escape_id (nm_setting_connection_get_id (s_con));
ifcfg_name = g_strdup_printf (IFCFG_DIR "/ifcfg-%s", escaped);
ifcfg = svCreateFile (ifcfg_name);
g_free (escaped);
}
if (!ifcfg) {
g_set_error (error, ifcfg_plugin_error_quark (), 0,
"Failed to open/create ifcfg file '%s'", ifcfg_name);
goto out;
}
type = nm_setting_connection_get_connection_type (s_con);
if (!type) {
g_set_error (error, ifcfg_plugin_error_quark (), 0,
"Missing connection type!");
goto out;
}
if (!strcmp (type, NM_SETTING_WIRED_SETTING_NAME)) {
} else if (!strcmp (type, NM_SETTING_WIRELESS_SETTING_NAME)) {
} else {
g_set_error (error, ifcfg_plugin_error_quark (), 0,
"Can't write connection type '%s'", type);
goto out;
}
write_connection_setting (s_con, ifcfg);
out:
g_free (ifcfg_name);
return success;
}

View file

@ -0,0 +1,34 @@
/* -*- 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) 2009 Red Hat, Inc.
*/
#ifndef _WRITER_H_
#define _WRITER_H_
#include <sys/types.h>
#include <glib.h>
#include <nm-connection.h>
gboolean write_connection (NMConnection *connection,
const char *ifcfg_dir,
const char *filename,
const char *keyfile,
GError **error);
#endif /* _WRITER_H_ */