2007-11-27 Dan Williams <dcbw@redhat.com>

* system-settings/plugins/ifcfg/Makefile.am
	  system-settings/plugins/ifcfg/parser.c
	  system-settings/plugins/ifcfg/parser.h
	  system-settings/plugins/ifcfg/plugin.c
		- Parse wireless connections too

	* system-settings/src/dbus-settings.c
	  system-settings/src/dbus-settings.h
	  system-settings/src/main.c
		- Handle connection update/removal if the plugin supports it



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3112 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Dan Williams 2007-11-27 19:25:46 +00:00
parent 7f21476233
commit 9e53dc83b0
8 changed files with 512 additions and 64 deletions

View file

@ -1,3 +1,16 @@
2007-11-27 Dan Williams <dcbw@redhat.com>
* system-settings/plugins/ifcfg/Makefile.am
system-settings/plugins/ifcfg/parser.c
system-settings/plugins/ifcfg/parser.h
system-settings/plugins/ifcfg/plugin.c
- Parse wireless connections too
* system-settings/src/dbus-settings.c
system-settings/src/dbus-settings.h
system-settings/src/main.c
- Handle connection update/removal if the plugin supports it
2007-11-27 Dan Williams <dcbw@redhat.com>
* src/nm-dbus-manager.h

View file

@ -12,6 +12,7 @@ libnm_settings_plugin_ifcfg_la_SOURCES = \
libnm_settings_plugin_ifcfg_la_CPPFLAGS = \
$(GLIB_CFLAGS) \
$(GMODULE_CFLAGS) \
$(DBUS_CFLAGS) \
-DG_DISABLE_DEPRECATED \
-I${top_srcdir}/system-settings/src \
-I$(top_srcdir)/include \

View file

@ -24,6 +24,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <glib.h>
@ -32,11 +33,24 @@
#include <nm-setting-connection.h>
#include <nm-setting-ip4-config.h>
#include <nm-setting-wired.h>
#include <nm-setting-wireless.h>
#include <nm-utils.h>
#include "shvar.h"
#include "parser.h"
#include "plugin.h"
static GQuark
ifcfg_error_quark (void)
{
static GQuark error_quark = 0;
if (G_UNLIKELY (error_quark == 0))
error_quark = g_quark_from_static_string ("ifcfg-plugin-error-quark");
return error_quark;
}
char *
parser_get_current_profile_name (void)
{
@ -67,11 +81,15 @@ get_int (const char *str, int *value)
}
static NMSetting *
make_connection_setting (const char *file, shvarFile *ifcfg, const char *type)
make_connection_setting (const char *file,
shvarFile *ifcfg,
const char *type,
const char *suggested)
{
NMSettingConnection *s_con;
char *basename = NULL;
int len;
char *ifcfg_name;
basename = g_path_get_basename (file);
if (!basename)
@ -90,7 +108,21 @@ make_connection_setting (const char *file, shvarFile *ifcfg, const char *type)
s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
s_con->id = g_strdup_printf ("System %s", basename + strlen (IFCFG_TAG));
ifcfg_name = (char *) (basename + strlen (IFCFG_TAG));
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)) {
s_con->id = g_strdup_printf ("System %s (%s)", suggested, ifcfg_name);
ifcfg_name = NULL;
}
}
if (ifcfg_name)
s_con->id = g_strdup_printf ("System %s", ifcfg_name);
s_con->type = g_strdup (type);
s_con->autoconnect = TRUE;
@ -165,12 +197,11 @@ out:
}
static NMSetting *
make_ip4_setting (shvarFile *ifcfg)
make_ip4_setting (shvarFile *ifcfg, GError **error)
{
NMSettingIP4Config *s_ip4;
char *value;
NMSettingIP4Config *s_ip4 = NULL;
char *value = NULL;
NMSettingIP4Address tmp = { 0, 0, 0 };
char *ip4 = NULL, *gw = NULL, *mask = NULL;
gboolean manual = TRUE;
value = svGetValue (ifcfg, "BOOTPROTO");
@ -182,34 +213,43 @@ make_ip4_setting (shvarFile *ifcfg)
return NULL;
}
ip4 = svGetValue (ifcfg, "IPADDR");
if (ip4) {
value = svGetValue (ifcfg, "IPADDR");
if (value) {
struct in_addr ip4_addr;
if (inet_pton (AF_INET, ip4, &ip4_addr))
if (inet_pton (AF_INET, value, &ip4_addr))
tmp.address = ip4_addr.s_addr;
else
g_warning ("Invalid IP4 address '%s'", ip4);
g_free (ip4);
else {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid IP4 address '%s'", value);
goto error;
}
g_free (value);
}
gw = svGetValue (ifcfg, "GATEWAY");
if (gw) {
value = svGetValue (ifcfg, "GATEWAY");
if (value) {
struct in_addr gw_addr;
if (inet_pton (AF_INET, gw, &gw_addr))
if (inet_pton (AF_INET, value, &gw_addr))
tmp.gateway = gw_addr.s_addr;
else
g_warning ("Invalid IP4 gateway '%s'", gw);
g_free (gw);
else {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid IP4 gateway '%s'", value);
goto error;
}
g_free (value);
}
mask = svGetValue (ifcfg, "NETMASK");
if (mask) {
value = svGetValue (ifcfg, "NETMASK");
if (value) {
struct in_addr mask_addr;
if (inet_pton (AF_INET, mask, &mask_addr))
if (inet_pton (AF_INET, value, &mask_addr))
tmp.netmask = mask_addr.s_addr;
else
g_warning ("Invalid IP4 netmask '%s'", mask);
g_free (mask);
else {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid IP4 netmask '%s'", value);
goto error;
}
g_free (value);
}
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
@ -223,18 +263,315 @@ make_ip4_setting (shvarFile *ifcfg)
read_profile_resolv_conf (s_ip4);
return (NMSetting *) s_ip4;
return NM_SETTING (s_ip4);
error:
g_free (value);
if (s_ip4)
g_object_unref (s_ip4);
return NULL;
}
/*
* utils_bin2hexstr
*
* Convert a byte-array into a hexadecimal string.
*
* Code originally by Alex Larsson <alexl@redhat.com> and
* copyright Red Hat, Inc. under terms of the LGPL.
*
*/
static char *
utils_bin2hexstr (const char *bytes, int len, int final_len)
{
static char hex_digits[] = "0123456789abcdef";
char * result;
int i;
g_return_val_if_fail (bytes != NULL, NULL);
g_return_val_if_fail (len > 0, NULL);
g_return_val_if_fail (len < 256, NULL); /* Arbitrary limit */
result = g_malloc0 (len * 2 + 1);
for (i = 0; i < len; i++)
{
result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf];
result[2*i+1] = hex_digits[bytes[i] & 0xf];
}
/* Cut converted key off at the correct length for this cipher type */
if (final_len > -1)
result[final_len] = '\0';
return result;
}
static char *
get_one_wep_key (shvarFile *ifcfg, guint8 idx, GError **error)
{
char *shvar_key;
char *key = NULL;
char *value = NULL;
g_return_val_if_fail (idx <= 3, NULL);
shvar_key = g_strdup_printf ("KEY%d", idx);
value = svGetValue (ifcfg, shvar_key);
if (!value)
goto out;
/* Validate keys */
if (strlen (value) == 10 || strlen (value) == 26) {
/* Hexadecimal WEP key */
char *p = value;
while (*p) {
if (!g_ascii_isxdigit (*p)) {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid hexadecimal WEP key.");
goto out;
}
p++;
}
key = g_strdup (value);
} else if (strlen (value) == 5 || strlen (value) == 13) {
/* ASCII passphrase */
char *p = value;
while (*p) {
if (!isascii ((int) (*p))) {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid ASCII WEP passphrase.");
goto out;
}
p++;
}
value = utils_bin2hexstr (value, strlen (value), strlen (value) * 2);
} else {
g_set_error (error, ifcfg_error_quark (), 0, "Invalid WEP key length.");
}
out:
g_free (value);
g_free (shvar_key);
return key;
}
#define READ_WEP_KEY(idx) \
{ \
char *key = get_one_wep_key (ifcfg, idx, error); \
if (*error) \
goto error; \
if (key) { \
g_object_set_data_full (G_OBJECT (s_wireless_sec), \
NM_SETTING_WIRELESS_SECURITY_WEP_KEY##idx, \
key, \
g_free); \
have_key = TRUE; \
} \
}
static NMSetting *
make_wireless_security_setting (shvarFile *ifcfg, GError **error)
{
NMSettingWirelessSecurity *s_wireless_sec;
gboolean have_key = FALSE;
char *value;
s_wireless_sec = NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
READ_WEP_KEY(0)
READ_WEP_KEY(1)
READ_WEP_KEY(2)
READ_WEP_KEY(3)
value = svGetValue (ifcfg, "DEFAULTKEY");
if (value) {
gboolean success;
int key_idx = 0;
success = get_int (value, &key_idx);
if (success && (key_idx >= 0) && (key_idx <= 3))
s_wireless_sec->wep_tx_keyidx = key_idx;
else {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid defualt WEP key '%s'", value);
g_free (value);
goto error;
}
g_free (value);
}
value = svGetValue (ifcfg, "SECURITYMODE");
if (value) {
char *lcase;
lcase = g_ascii_strdown (value, -1);
g_free (value);
if (!strcmp (lcase, "open")) {
s_wireless_sec->auth_alg = g_strdup ("open");
} else if (!strcmp (lcase, "restricted")) {
s_wireless_sec->auth_alg = g_strdup ("shared");
} else {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid WEP authentication algoritm '%s'",
lcase);
g_free (lcase);
goto error;
}
g_free (lcase);
}
if (have_key)
s_wireless_sec->key_mgmt = g_strdup ("none");
return NM_SETTING (s_wireless_sec);
error:
if (s_wireless_sec)
g_object_unref (s_wireless_sec);
return NULL;
}
static NMSetting *
make_wired_setting (shvarFile *ifcfg)
make_wireless_setting (shvarFile *ifcfg,
NMSetting *security,
GError **error)
{
NMSettingWireless *s_wireless;
char *value;
s_wireless = NM_SETTING_WIRELESS (nm_setting_wireless_new ());
value = svGetValue (ifcfg, "ESSID");
if (value) {
gsize len = strlen (value);
if (len > 32 || len == 0) {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid SSID '%s' (size %d not between 1 and 32 inclusive)",
value, len);
goto error;
}
s_wireless->ssid = g_byte_array_sized_new (strlen (value));
g_byte_array_append (s_wireless->ssid, (const guint8 *) value, len);
g_free (value);
}
value = svGetValue (ifcfg, "MODE");
if (value) {
char *lcase;
lcase = g_ascii_strdown (value, -1);
g_free (value);
if (!strcmp (lcase, "ad-hoc")) {
s_wireless->mode = g_strdup ("adhoc");
} else if (!strcmp (lcase, "managed")) {
s_wireless->mode = g_strdup ("infrastructure");
} else {
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid mode '%s' (not ad-hoc or managed)",
lcase);
g_free (lcase);
goto error;
}
g_free (lcase);
}
if (security)
s_wireless->security = g_strdup (NM_SETTING_WIRELESS_SECURITY_SETTING_NAME);
// FIXME: channel/freq, other L2 parameters like RTS
return NM_SETTING (s_wireless);
error:
if (s_wireless)
g_object_unref (s_wireless);
return NULL;
}
static NMConnection *
wireless_connection_from_ifcfg (const char *file, shvarFile *ifcfg, GError **error)
{
NMConnection *connection = NULL;
NMSetting *con_setting = NULL;
NMSetting *wireless_setting = NULL;
NMSettingWireless *tmp;
NMSetting *security_setting = NULL;
char *printable_ssid = NULL;
g_return_val_if_fail (file != NULL, NULL);
g_return_val_if_fail (ifcfg != NULL, NULL);
g_return_val_if_fail (error != NULL, NULL);
g_return_val_if_fail (*error == NULL, NULL);
connection = nm_connection_new ();
if (!connection) {
g_set_error (error, ifcfg_error_quark (), 0,
"Failed to allocate new connection for %s.", file);
return NULL;
}
/* Wireless security */
security_setting = make_wireless_security_setting (ifcfg, error);
if (*error)
goto error;
if (security_setting)
nm_connection_add_setting (connection, security_setting);
/* Wireless */
wireless_setting = make_wireless_setting (ifcfg, security_setting, error);
if (!wireless_setting)
goto error;
nm_connection_add_setting (connection, wireless_setting);
tmp = NM_SETTING_WIRELESS (wireless_setting);
printable_ssid = nm_utils_ssid_to_utf8 ((const char *) tmp->ssid->data,
(guint32) tmp->ssid->len);
con_setting = make_connection_setting (file, ifcfg,
NM_SETTING_WIRELESS_SETTING_NAME,
printable_ssid);
if (!con_setting) {
g_set_error (error, ifcfg_error_quark (), 0,
"Failed to create connection setting.");
goto error;
}
nm_connection_add_setting (connection, con_setting);
if (!nm_connection_verify (connection)) {
g_set_error (error, ifcfg_error_quark (), 0,
"Connection from %s was invalid.", file);
goto error;
}
return connection;
error:
g_free (printable_ssid);
g_object_unref (connection);
if (con_setting)
g_object_unref (con_setting);
if (wireless_setting)
g_object_unref (wireless_setting);
return NULL;
}
static NMSetting *
make_wired_setting (shvarFile *ifcfg, GError **error)
{
NMSettingWired *s_wired;
char *value;
int mtu;
s_wired = (NMSettingWired *) nm_setting_wired_new ();
s_wired = NM_SETTING_WIRED (nm_setting_wired_new ());
value = svGetValue (ifcfg, "MTU");
if (value) {
@ -242,7 +579,10 @@ make_wired_setting (shvarFile *ifcfg)
if (mtu >= 0 && mtu < 65536)
s_wired->mtu = mtu;
} else {
g_warning ("Invalid MTU '%s'", value);
g_set_error (error, ifcfg_error_quark (), 0,
"Invalid MTU '%s'", value);
g_object_unref (s_wired);
s_wired = NULL;
}
g_free (value);
}
@ -251,7 +591,7 @@ make_wired_setting (shvarFile *ifcfg)
}
static NMConnection *
wired_connection_from_ifcfg (const char *file, shvarFile *ifcfg)
wired_connection_from_ifcfg (const char *file, shvarFile *ifcfg, GError **error)
{
NMConnection *connection = NULL;
NMSetting *con_setting = NULL;
@ -262,26 +602,28 @@ wired_connection_from_ifcfg (const char *file, shvarFile *ifcfg)
connection = nm_connection_new ();
if (!connection) {
g_warning ("Failed to allocate new connection for %s.", file);
g_set_error (error, ifcfg_error_quark (), 0,
"Failed to allocate new connection for %s.", file);
return NULL;
}
con_setting = make_connection_setting (file, ifcfg, NM_SETTING_WIRED_SETTING_NAME);
con_setting = make_connection_setting (file, ifcfg, NM_SETTING_WIRED_SETTING_NAME, NULL);
if (!con_setting) {
g_warning ("Failed to create connection setting.");
g_set_error (error, ifcfg_error_quark (), 0,
"Failed to create connection setting.");
goto error;
}
nm_connection_add_setting (connection, con_setting);
wired_setting = make_wired_setting (ifcfg);
if (!wired_setting) {
g_warning ("Failed to create wired setting.");
wired_setting = make_wired_setting (ifcfg, error);
if (!wired_setting)
goto error;
}
nm_connection_add_setting (connection, wired_setting);
if (!nm_connection_verify (connection)) {
g_warning ("Connection from %s was invalid.", file);
g_set_error (error, ifcfg_error_quark (), 0,
"Connection from %s was invalid.", file);
goto error;
}
@ -298,7 +640,7 @@ error:
NMConnection *
parser_parse_file (const char *file,
char **err)
GError **error)
{
NMConnection *connection = NULL;
shvarFile *parsed;
@ -309,13 +651,15 @@ parser_parse_file (const char *file,
parsed = svNewFile(file);
if (!parsed) {
*err = g_strdup_printf ("Couldn't parse file '%s'", file);
g_set_error (error, ifcfg_error_quark (), 0,
"Couldn't parse file '%s'", file);
return NULL;
}
type = svGetValue (parsed, "TYPE");
if (!type) {
*err = g_strdup_printf ("File '%s' didn't have a TYPE key.", file);
g_set_error (error, ifcfg_error_quark (), 0,
"File '%s' didn't have a TYPE key.", file);
goto done;
}
@ -334,19 +678,27 @@ parser_parse_file (const char *file,
g_free (lower);
}
if (!strcmp (type, "Ethernet")) {
connection = wired_connection_from_ifcfg (file, parsed);
} else if (!strcmp (type, "Wireless")) {
// connection = wireless_connection_from_ifcfg (file, parsed);
if (!strcmp (type, "Ethernet"))
connection = wired_connection_from_ifcfg (file, parsed, error);
else if (!strcmp (type, "Wireless"))
connection = wireless_connection_from_ifcfg (file, parsed, error);
else {
g_set_error (error, ifcfg_error_quark (), 0,
"Unknown connection type '%s'", type);
}
g_free (type);
if (connection) {
NMSetting *s_ip4;
s_ip4 = make_ip4_setting (parsed);
if (s_ip4)
s_ip4 = make_ip4_setting (parsed, error);
if (*error) {
g_object_unref (connection);
connection = NULL;
} else if (s_ip4) {
nm_connection_add_setting (connection, s_ip4);
}
}
done:

View file

@ -28,7 +28,7 @@
#define BAK_TAG ".bak"
NMConnection * parser_parse_file (const char *file, char **err);
NMConnection * parser_parse_file (const char *file, GError **error);
char * parser_get_current_profile_name (void);

View file

@ -76,7 +76,7 @@ parse_files (gpointer data)
while ((item = g_dir_read_name (dir))) {
NMConnection *connection;
char *err = NULL;
GError *error = NULL;
char *filename;
if (strncmp (item, IFCFG_TAG, strlen (IFCFG_TAG)))
@ -88,7 +88,7 @@ parse_files (gpointer data)
PLUGIN_PRINT (PLUGIN_NAME, "parsing %s ... ", filename);
if ((connection = parser_parse_file (filename, &err))) {
if ((connection = parser_parse_file (filename, &error))) {
NMSettingConnection *s_con;
s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
@ -101,7 +101,9 @@ parse_files (gpointer data)
connection);
added = TRUE;
} else {
PLUGIN_PRINT (PLUGIN_NAME, " error: %s", err ? err : "(unknown)");
PLUGIN_PRINT (PLUGIN_NAME, " error: %s",
error->message ? error->message : "(unknown)");
g_clear_error (&error);
}
g_free (filename);

View file

@ -182,13 +182,88 @@ nm_sysconfig_settings_new (DBusGConnection *g_conn)
void
nm_sysconfig_settings_add_connection (NMSysconfigSettings *settings,
NMSysconfigConnectionSettings *connection)
NMConnection *connection,
DBusGConnection *g_connection)
{
g_return_if_fail (NM_IS_SYSCONFIG_SETTINGS (settings));
g_return_if_fail (NM_IS_SYSCONFIG_CONNECTION_SETTINGS (connection));
NMSysconfigConnectionSettings *exported;
settings->connections = g_slist_append (settings->connections, connection);
g_return_if_fail (NM_IS_SYSCONFIG_SETTINGS (settings));
g_return_if_fail (NM_IS_CONNECTION (connection));
exported = nm_sysconfig_connection_settings_new (connection, g_connection);
if (!exported) {
g_warning ("%s: couldn't export the connection!", __func__);
return;
}
settings->connections = g_slist_append (settings->connections, exported);
nm_settings_signal_new_connection (NM_SETTINGS (settings),
NM_CONNECTION_SETTINGS (connection));
NM_CONNECTION_SETTINGS (exported));
}
static void
remove_connection (NMSysconfigSettings *settings,
NMConnection *connection)
{
GSList *iter;
g_return_if_fail (NM_IS_SYSCONFIG_SETTINGS (settings));
g_return_if_fail (NM_IS_CONNECTION (connection));
for (iter = settings->connections; iter; iter = g_slist_next (iter)) {
NMSysconfigConnectionSettings *item = NM_SYSCONFIG_CONNECTION_SETTINGS (iter->data);
if (item->connection == connection) {
settings->connections = g_slist_remove (settings->connections, iter);
nm_connection_settings_signal_removed (NM_CONNECTION_SETTINGS (item));
g_object_unref (item);
g_slist_free (iter);
break;
}
}
}
void
nm_sysconfig_settings_remove_connection (NMSysconfigSettings *settings,
NMConnection *connection)
{
remove_connection (settings, connection);
}
void
nm_sysconfig_settings_update_connection (NMSysconfigSettings *settings,
NMConnection *connection)
{
GHashTable *hash;
GSList *iter;
NMSysconfigConnectionSettings *found = NULL;
g_return_if_fail (NM_IS_SYSCONFIG_SETTINGS (settings));
g_return_if_fail (NM_IS_CONNECTION (connection));
for (iter = settings->connections; iter; iter = g_slist_next (iter)) {
NMSysconfigConnectionSettings *item = NM_SYSCONFIG_CONNECTION_SETTINGS (iter->data);
if (item->connection == connection) {
found = item;
break;
}
}
if (!found) {
g_warning ("%s: cannot update unknown connection", __func__);
return;
}
/* If the connection is no longer valid, it gets removed */
if (!nm_connection_verify (connection)) {
remove_connection (settings, connection);
return;
}
hash = nm_connection_to_hash (connection);
nm_connection_settings_signal_updated (NM_CONNECTION_SETTINGS (found), hash);
g_hash_table_destroy (hash);
}

View file

@ -79,6 +79,16 @@ struct _NMSysconfigSettingsClass
};
GType nm_sysconfig_settings_get_type (void);
NMSysconfigSettings *nm_sysconfig_settings_new (DBusGConnection *g_conn);
void nm_sysconfig_settings_add_connection (NMSysconfigSettings *settings,
NMSysconfigConnectionSettings *connection);
NMConnection *connection,
DBusGConnection *g_connection);
void nm_sysconfig_settings_remove_connection (NMSysconfigSettings *settings,
NMConnection *connection);
void nm_sysconfig_settings_update_connection (NMSysconfigSettings *settings,
NMConnection *connection);

View file

@ -76,14 +76,7 @@ connection_added_cb (NMSystemConfigInterface *config,
NMConnection *connection,
Application *app)
{
NMSysconfigConnectionSettings *exported;
exported = nm_sysconfig_connection_settings_new (connection, app->g_connection);
if (!exported) {
g_warning ("%s: couldn't export the connection!", __func__);
return;
}
nm_sysconfig_settings_add_connection (app->settings, exported);
nm_sysconfig_settings_add_connection (app->settings, connection, app->g_connection);
}
static void
@ -91,6 +84,7 @@ connection_removed_cb (NMSystemConfigInterface *config,
NMConnection *connection,
Application *app)
{
nm_sysconfig_settings_remove_connection (app->settings, connection);
}
static void
@ -98,6 +92,7 @@ connection_updated_cb (NMSystemConfigInterface *config,
NMConnection *connection,
Application *app)
{
nm_sysconfig_settings_update_connection (app->settings, connection);
}
static void