mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-04 04:47:59 +02:00
ifcfg-rh: read multiple IP addresses, more DNS servers, write more IP4 stuff
Add a simple testcase for write too.
This commit is contained in:
parent
44964d3b5e
commit
13c5e660de
6 changed files with 399 additions and 131 deletions
|
|
@ -329,11 +329,11 @@ update (NMExportedConnection *exported, GHashTable *new_settings, GError **error
|
|||
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);
|
||||
success = writer_update_connection (connection,
|
||||
IFCFG_DIR,
|
||||
priv->filename,
|
||||
priv->keyfile,
|
||||
error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -476,7 +476,7 @@ add_connection (NMSystemConfigInterface *config,
|
|||
NMConnection *connection,
|
||||
GError **error)
|
||||
{
|
||||
return write_connection (connection, IFCFG_DIR, NULL, NULL, error);
|
||||
return writer_new_connection (connection, IFCFG_DIR, NULL, error);
|
||||
}
|
||||
|
||||
#define SC_NETWORK_FILE SYSCONFDIR"/sysconfig/network"
|
||||
|
|
|
|||
|
|
@ -167,12 +167,14 @@ make_connection_setting (const char *file,
|
|||
value = svGetValue (ifcfg, "LAST_CONNECT", FALSE);
|
||||
if (value) {
|
||||
unsigned long int tmp;
|
||||
guint64 timestamp;
|
||||
|
||||
errno = 0;
|
||||
tmp = strtoul (value, NULL, 10);
|
||||
if (errno == 0)
|
||||
g_object_set (s_con, NM_SETTING_CONNECTION_TIMESTAMP, tmp, NULL);
|
||||
else
|
||||
if (errno == 0) {
|
||||
timestamp = (guint64) tmp;
|
||||
g_object_set (s_con, NM_SETTING_CONNECTION_TIMESTAMP, timestamp, NULL);
|
||||
} else
|
||||
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: invalid LAST_CONNECT time");
|
||||
g_free (value);
|
||||
}
|
||||
|
|
@ -181,58 +183,163 @@ make_connection_setting (const char *file,
|
|||
return NM_SETTING (s_con);
|
||||
}
|
||||
|
||||
static void
|
||||
get_one_ip4_addr (shvarFile *ifcfg,
|
||||
static gboolean
|
||||
read_ip4_address (shvarFile *ifcfg,
|
||||
const char *tag,
|
||||
guint32 *out_addr,
|
||||
GError **error)
|
||||
{
|
||||
char *value = NULL;
|
||||
struct in_addr ip4_addr;
|
||||
gboolean success = FALSE;
|
||||
|
||||
g_return_if_fail (ifcfg != NULL);
|
||||
g_return_if_fail (tag != NULL);
|
||||
g_return_if_fail (out_addr != NULL);
|
||||
g_return_if_fail (*out_addr == 0);
|
||||
g_return_if_fail (error != NULL);
|
||||
g_return_if_fail (*error == NULL);
|
||||
g_return_val_if_fail (ifcfg != NULL, FALSE);
|
||||
g_return_val_if_fail (tag != NULL, FALSE);
|
||||
g_return_val_if_fail (out_addr != NULL, FALSE);
|
||||
g_return_val_if_fail (error != NULL, FALSE);
|
||||
g_return_val_if_fail (*error == NULL, FALSE);
|
||||
|
||||
*out_addr = 0;
|
||||
|
||||
value = svGetValue (ifcfg, tag, FALSE);
|
||||
if (!value)
|
||||
return;
|
||||
return TRUE;
|
||||
|
||||
if (inet_pton (AF_INET, value, &ip4_addr) > 0)
|
||||
if (inet_pton (AF_INET, value, &ip4_addr) > 0) {
|
||||
*out_addr = ip4_addr.s_addr;
|
||||
else {
|
||||
success = TRUE;
|
||||
} else {
|
||||
g_set_error (error, ifcfg_plugin_error_quark (), 0,
|
||||
"Invalid %s IP4 address '%s'", tag, value);
|
||||
}
|
||||
g_free (value);
|
||||
return success;
|
||||
}
|
||||
|
||||
#define GET_ONE_DNS(tag) \
|
||||
{ \
|
||||
guint32 dns = 0; \
|
||||
get_one_ip4_addr (ifcfg, tag, &dns, error); \
|
||||
if (*error) \
|
||||
goto error; \
|
||||
if (dns) { \
|
||||
if (!nm_setting_ip4_config_add_dns (s_ip4, dns)) \
|
||||
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: duplicate DNS server %s", tag); \
|
||||
} \
|
||||
static NMIP4Address *
|
||||
read_full_ip4_address (shvarFile *ifcfg,
|
||||
const char *network_file,
|
||||
guint32 which,
|
||||
GError **error)
|
||||
{
|
||||
NMIP4Address *addr;
|
||||
char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag;
|
||||
guint32 tmp;
|
||||
gboolean success = FALSE;
|
||||
shvarFile *network_ifcfg;
|
||||
char *value;
|
||||
|
||||
g_return_val_if_fail (which > 0, NULL);
|
||||
g_return_val_if_fail (ifcfg != NULL, NULL);
|
||||
g_return_val_if_fail (network_file != NULL, NULL);
|
||||
|
||||
addr = nm_ip4_address_new ();
|
||||
if (which == 1) {
|
||||
ip_tag = g_strdup ("IPADDR");
|
||||
prefix_tag = g_strdup ("PREFIX");
|
||||
netmask_tag = g_strdup ("NETMASK");
|
||||
gw_tag = g_strdup ("GATEWAY");
|
||||
} else {
|
||||
ip_tag = g_strdup_printf ("IPADDR%u", which);
|
||||
prefix_tag = g_strdup_printf ("PREFIX%u", which);
|
||||
netmask_tag = g_strdup_printf ("NETMASK%u", which);
|
||||
gw_tag = g_strdup_printf ("GATEWAY%u", which);
|
||||
}
|
||||
|
||||
|
||||
/* IP address */
|
||||
if (!read_ip4_address (ifcfg, ip_tag, &tmp, error))
|
||||
goto error;
|
||||
if (!tmp) {
|
||||
nm_ip4_address_unref (addr);
|
||||
addr = NULL;
|
||||
success = TRUE; /* done */
|
||||
goto error;
|
||||
}
|
||||
nm_ip4_address_set_address (addr, tmp);
|
||||
|
||||
/* Gateway */
|
||||
if (!read_ip4_address (ifcfg, gw_tag, &tmp, error))
|
||||
goto error;
|
||||
if (tmp)
|
||||
nm_ip4_address_set_gateway (addr, tmp);
|
||||
else {
|
||||
gboolean read_success;
|
||||
|
||||
/* If no gateway in the ifcfg, try /etc/sysconfig/network instead */
|
||||
network_ifcfg = svNewFile (network_file);
|
||||
if (network_ifcfg) {
|
||||
read_success = read_ip4_address (network_ifcfg, "GATEWAY", &tmp, error);
|
||||
svCloseFile (network_ifcfg);
|
||||
if (!read_success)
|
||||
goto error;
|
||||
nm_ip4_address_set_gateway (addr, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* Prefix */
|
||||
value = svGetValue (ifcfg, prefix_tag, FALSE);
|
||||
if (value) {
|
||||
long int prefix;
|
||||
|
||||
errno = 0;
|
||||
prefix = strtol (value, NULL, 10);
|
||||
if (errno || prefix <= 0 || prefix > 32) {
|
||||
g_set_error (error, ifcfg_plugin_error_quark (), 0,
|
||||
"Invalid IP4 prefix '%s'", value);
|
||||
g_free (value);
|
||||
goto error;
|
||||
}
|
||||
nm_ip4_address_set_prefix (addr, (guint32) prefix);
|
||||
g_free (value);
|
||||
}
|
||||
|
||||
/* Fall back to NETMASK if no PREFIX was specified */
|
||||
if (!nm_ip4_address_get_prefix (addr)) {
|
||||
if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error))
|
||||
goto error;
|
||||
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp));
|
||||
}
|
||||
|
||||
/* Validate the prefix */
|
||||
if ( !nm_ip4_address_get_prefix (addr)
|
||||
|| nm_ip4_address_get_prefix (addr) > 32) {
|
||||
g_set_error (error, ifcfg_plugin_error_quark (), 0,
|
||||
"Missing or invalid IP4 prefix '%d'",
|
||||
nm_ip4_address_get_prefix (addr));
|
||||
goto error;
|
||||
}
|
||||
|
||||
success = TRUE;
|
||||
|
||||
error:
|
||||
if (!success) {
|
||||
nm_ip4_address_unref (addr);
|
||||
addr = NULL;
|
||||
}
|
||||
|
||||
g_free (ip_tag);
|
||||
g_free (prefix_tag);
|
||||
g_free (netmask_tag);
|
||||
g_free (gw_tag);
|
||||
return addr;
|
||||
}
|
||||
|
||||
static NMSetting *
|
||||
make_ip4_setting (shvarFile *ifcfg, const char *network_file, GError **error)
|
||||
{
|
||||
NMSettingIP4Config *s_ip4 = NULL;
|
||||
char *value = NULL;
|
||||
NMIP4Address *addr = NULL;
|
||||
char *method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL;
|
||||
guint32 netmask = 0, tmp = 0;
|
||||
guint32 i;
|
||||
shvarFile *network_ifcfg;
|
||||
gboolean never_default = FALSE;
|
||||
gboolean never_default = FALSE, tmp_success;
|
||||
|
||||
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
|
||||
if (!s_ip4) {
|
||||
g_set_error (error, ifcfg_plugin_error_quark (), 0,
|
||||
"Could not allocate IP4 setting");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
network_ifcfg = svNewFile (network_file);
|
||||
if (network_ifcfg) {
|
||||
|
|
@ -259,7 +366,6 @@ make_ip4_setting (shvarFile *ifcfg, const char *network_file, GError **error)
|
|||
method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
|
||||
else if (!g_ascii_strcasecmp (value, "autoip")) {
|
||||
g_free (value);
|
||||
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
|
||||
g_object_set (s_ip4,
|
||||
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
|
||||
NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default,
|
||||
|
|
@ -267,7 +373,6 @@ make_ip4_setting (shvarFile *ifcfg, const char *network_file, GError **error)
|
|||
return NM_SETTING (s_ip4);
|
||||
} else if (!g_ascii_strcasecmp (value, "shared")) {
|
||||
g_free (value);
|
||||
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
|
||||
g_object_set (s_ip4,
|
||||
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED,
|
||||
NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default,
|
||||
|
|
@ -295,94 +400,56 @@ make_ip4_setting (shvarFile *ifcfg, const char *network_file, GError **error)
|
|||
g_free (tmp_netmask);
|
||||
}
|
||||
|
||||
/* Handle manual settings */
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) {
|
||||
addr = nm_ip4_address_new ();
|
||||
|
||||
tmp = 0;
|
||||
get_one_ip4_addr (ifcfg, "IPADDR", &tmp, error);
|
||||
if (*error)
|
||||
goto error;
|
||||
nm_ip4_address_set_address (addr, tmp);
|
||||
|
||||
tmp = 0;
|
||||
get_one_ip4_addr (ifcfg, "GATEWAY", &tmp, error);
|
||||
if (*error)
|
||||
goto error;
|
||||
nm_ip4_address_set_gateway (addr, tmp);
|
||||
|
||||
/* If no gateway in the ifcfg, try /etc/sysconfig/network instead */
|
||||
if (!nm_ip4_address_get_gateway (addr)) {
|
||||
network_ifcfg = svNewFile (network_file);
|
||||
if (network_ifcfg) {
|
||||
tmp = 0;
|
||||
get_one_ip4_addr (network_ifcfg, "GATEWAY", &tmp, error);
|
||||
svCloseFile (network_ifcfg);
|
||||
if (*error)
|
||||
goto error;
|
||||
nm_ip4_address_set_gateway (addr, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
value = svGetValue (ifcfg, "PREFIX", FALSE);
|
||||
if (value) {
|
||||
long int prefix;
|
||||
|
||||
errno = 0;
|
||||
prefix = strtol (value, NULL, 10);
|
||||
if (errno || prefix <= 0 || prefix > 32) {
|
||||
g_set_error (error, ifcfg_plugin_error_quark (), 0,
|
||||
"Invalid IP4 prefix '%s'", value);
|
||||
g_free (value);
|
||||
goto error;
|
||||
}
|
||||
nm_ip4_address_set_prefix (addr, (guint32) prefix);
|
||||
g_free (value);
|
||||
}
|
||||
|
||||
/* Fall back to NETMASK if no PREFIX was specified */
|
||||
if (!nm_ip4_address_get_prefix (addr)) {
|
||||
netmask = 0;
|
||||
get_one_ip4_addr (ifcfg, "NETMASK", &netmask, error);
|
||||
if (*error)
|
||||
goto error;
|
||||
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (netmask));
|
||||
}
|
||||
|
||||
/* Validate the prefix */
|
||||
if ( !nm_ip4_address_get_prefix (addr)
|
||||
|| nm_ip4_address_get_prefix (addr) > 32) {
|
||||
g_set_error (error, ifcfg_plugin_error_quark (), 0,
|
||||
"Missing or invalid IP4 prefix '%d'",
|
||||
nm_ip4_address_get_prefix (addr));
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
/* Yay, let's make an IP4 config */
|
||||
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
|
||||
g_object_set (s_ip4,
|
||||
NM_SETTING_IP4_CONFIG_METHOD, method,
|
||||
NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "PEERDNS", 1),
|
||||
NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "PEERDNS", TRUE),
|
||||
NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, !svTrueValue (ifcfg, "PEERROUTES", TRUE),
|
||||
NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default,
|
||||
NULL);
|
||||
|
||||
/* DHCP hostname for 'send host-name' option */
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
|
||||
/* Handle manual settings */
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) {
|
||||
NMIP4Address *addr;
|
||||
|
||||
for (i = 1; i < 256; i++) {
|
||||
addr = read_full_ip4_address (ifcfg, network_file, i, error);
|
||||
if (error && *error)
|
||||
goto error;
|
||||
if (!addr)
|
||||
break;
|
||||
|
||||
if (!nm_setting_ip4_config_add_address (s_ip4, addr))
|
||||
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: duplicate IP4 address");
|
||||
nm_ip4_address_unref (addr);
|
||||
}
|
||||
} else if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
|
||||
value = svGetValue (ifcfg, "DHCP_HOSTNAME", FALSE);
|
||||
if (value && strlen (value))
|
||||
g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, value, NULL);
|
||||
g_free (value);
|
||||
|
||||
value = svGetValue (ifcfg, "DHCP_CLIENT_IDENTIFIER", FALSE);
|
||||
if (value && strlen (value))
|
||||
g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, value, NULL);
|
||||
g_free (value);
|
||||
}
|
||||
|
||||
if (addr) {
|
||||
if (!nm_setting_ip4_config_add_address (s_ip4, addr))
|
||||
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: duplicate IP4 address");
|
||||
}
|
||||
/* DNS servers */
|
||||
for (i = 1, tmp_success = TRUE; i <= 10 && tmp_success; i++) {
|
||||
char *tag;
|
||||
guint32 dns;
|
||||
|
||||
GET_ONE_DNS("DNS1");
|
||||
GET_ONE_DNS("DNS2");
|
||||
GET_ONE_DNS("DNS3");
|
||||
tag = g_strdup_printf ("DNS%u", i);
|
||||
tmp_success = read_ip4_address (ifcfg, tag, &dns, error);
|
||||
if (!tmp_success) {
|
||||
g_free (tag);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (dns && !nm_setting_ip4_config_add_dns (s_ip4, dns))
|
||||
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: duplicate DNS server %s", tag);
|
||||
g_free (tag);
|
||||
}
|
||||
|
||||
/* DNS searches */
|
||||
value = svGetValue (ifcfg, "DOMAIN", FALSE);
|
||||
|
|
@ -424,16 +491,10 @@ make_ip4_setting (shvarFile *ifcfg, const char *network_file, GError **error)
|
|||
}
|
||||
}
|
||||
|
||||
if (addr)
|
||||
nm_ip4_address_unref (addr);
|
||||
|
||||
return NM_SETTING (s_ip4);
|
||||
|
||||
error:
|
||||
if (addr)
|
||||
nm_ip4_address_unref (addr);
|
||||
if (s_ip4)
|
||||
g_object_unref (s_ip4);
|
||||
g_object_unref (s_ip4);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@
|
|||
|
||||
#include "common.h"
|
||||
#include "reader.h"
|
||||
#include "writer.h"
|
||||
|
||||
typedef enum {
|
||||
CK_CA_CERT = 0,
|
||||
|
|
@ -3257,6 +3258,143 @@ test_read_wifi_wep_eap_ttls_chap (void)
|
|||
g_object_unref (connection);
|
||||
}
|
||||
|
||||
static void
|
||||
test_write_wired_static (void)
|
||||
{
|
||||
NMConnection *connection;
|
||||
NMConnection *reread;
|
||||
NMSettingConnection *s_con;
|
||||
NMSettingWired *s_wired;
|
||||
NMSettingIP4Config *s_ip4;
|
||||
static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd };
|
||||
GByteArray *mac;
|
||||
guint32 mtu = 1492;
|
||||
char *uuid;
|
||||
guint64 timestamp = 0x12344433L;
|
||||
const guint32 ip1 = htonl (0x01010103);
|
||||
const guint32 ip2 = htonl (0x01010105);
|
||||
const guint32 gw = htonl (0x01010101);
|
||||
const guint32 dns1 = htonl (0x04020201);
|
||||
const guint32 dns2 = htonl (0x04020202);
|
||||
const guint32 prefix = 24;
|
||||
const char *dns_search1 = "foobar.com";
|
||||
const char *dns_search2 = "lab.foobar.com";
|
||||
NMIP4Address *addr;
|
||||
gboolean success;
|
||||
GError *error = NULL;
|
||||
char *testfile = NULL;
|
||||
gboolean unmanaged = FALSE;
|
||||
char *keyfile = NULL;
|
||||
gboolean ignore_error = FALSE;
|
||||
|
||||
connection = nm_connection_new ();
|
||||
ASSERT (connection != NULL,
|
||||
"wired-static-write", "failed to allocate new connection");
|
||||
|
||||
/* Connection setting */
|
||||
s_con = (NMSettingConnection *) nm_setting_connection_new ();
|
||||
ASSERT (s_con != NULL,
|
||||
"wired-static-write", "failed to allocate new %s setting",
|
||||
NM_SETTING_CONNECTION_SETTING_NAME);
|
||||
nm_connection_add_setting (connection, NM_SETTING (s_con));
|
||||
|
||||
uuid = nm_utils_uuid_generate ();
|
||||
g_object_set (s_con,
|
||||
NM_SETTING_CONNECTION_ID, "Work Ethernet",
|
||||
NM_SETTING_CONNECTION_UUID, uuid,
|
||||
NM_SETTING_CONNECTION_AUTOCONNECT, TRUE,
|
||||
NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
|
||||
NM_SETTING_CONNECTION_TIMESTAMP, timestamp,
|
||||
NULL);
|
||||
g_free (uuid);
|
||||
|
||||
/* Wired setting */
|
||||
s_wired = (NMSettingWired *) nm_setting_wired_new ();
|
||||
ASSERT (s_con != NULL,
|
||||
"wired-static-write", "failed to allocate new %s setting",
|
||||
NM_SETTING_WIRED_SETTING_NAME);
|
||||
nm_connection_add_setting (connection, NM_SETTING (s_wired));
|
||||
|
||||
mac = g_byte_array_sized_new (sizeof (tmpmac));
|
||||
g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac));
|
||||
|
||||
g_object_set (s_wired,
|
||||
NM_SETTING_WIRED_MAC_ADDRESS, mac,
|
||||
NM_SETTING_WIRED_MTU, mtu,
|
||||
NULL);
|
||||
g_byte_array_free (mac, TRUE);
|
||||
|
||||
/* IP4 setting */
|
||||
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
|
||||
ASSERT (s_ip4 != NULL,
|
||||
"connection-write", "failed to allocate new %s setting",
|
||||
NM_SETTING_IP4_CONFIG_SETTING_NAME);
|
||||
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
|
||||
|
||||
g_object_set (s_ip4,
|
||||
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
|
||||
NULL);
|
||||
|
||||
addr = nm_ip4_address_new ();
|
||||
nm_ip4_address_set_address (addr, ip1);
|
||||
nm_ip4_address_set_prefix (addr, prefix);
|
||||
nm_ip4_address_set_gateway (addr, gw);
|
||||
nm_setting_ip4_config_add_address (s_ip4, addr);
|
||||
nm_ip4_address_unref (addr);
|
||||
|
||||
addr = nm_ip4_address_new ();
|
||||
nm_ip4_address_set_address (addr, ip2);
|
||||
nm_ip4_address_set_prefix (addr, prefix);
|
||||
nm_ip4_address_set_gateway (addr, gw);
|
||||
nm_setting_ip4_config_add_address (s_ip4, addr);
|
||||
nm_ip4_address_unref (addr);
|
||||
|
||||
nm_setting_ip4_config_add_dns (s_ip4, dns1);
|
||||
nm_setting_ip4_config_add_dns (s_ip4, dns2);
|
||||
|
||||
nm_setting_ip4_config_add_dns_search (s_ip4, dns_search1);
|
||||
nm_setting_ip4_config_add_dns_search (s_ip4, dns_search2);
|
||||
|
||||
ASSERT (nm_connection_verify (connection, &error) == TRUE,
|
||||
"wired-static-write", "failed to verify connection: %s",
|
||||
(error && error->message) ? error->message : "(unknown)");
|
||||
|
||||
/* Save the ifcfg */
|
||||
success = writer_new_connection (connection,
|
||||
TEST_DIR "/network-scripts/",
|
||||
&testfile,
|
||||
&error);
|
||||
ASSERT (success == TRUE,
|
||||
"wired-static-write", "failed to write connection to disk: %s",
|
||||
(error && error->message) ? error->message : "(unknown)");
|
||||
|
||||
ASSERT (testfile != NULL,
|
||||
"wired-static-write", "didn't get ifcfg file path back after writing connection");
|
||||
|
||||
/* re-read the connection for comparison */
|
||||
reread = connection_from_file (testfile,
|
||||
NULL,
|
||||
TYPE_ETHERNET,
|
||||
&unmanaged,
|
||||
&keyfile,
|
||||
&error,
|
||||
&ignore_error);
|
||||
unlink (testfile);
|
||||
|
||||
ASSERT (reread != NULL,
|
||||
"wired-static-write-reread", "failed to read %s: %s", testfile, error->message);
|
||||
|
||||
ASSERT (nm_connection_verify (reread, &error),
|
||||
"wired-static-write-reread-verify", "failed to verify %s: %s", testfile, error->message);
|
||||
|
||||
ASSERT (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT) == TRUE,
|
||||
"wired-static-write", "written and re-read connection weren't the same.");
|
||||
|
||||
g_free (testfile);
|
||||
g_object_unref (connection);
|
||||
g_object_unref (reread);
|
||||
}
|
||||
|
||||
#define TEST_IFCFG_WIFI_OPEN_SSID_BAD_HEX TEST_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-bad-hex"
|
||||
#define TEST_IFCFG_WIFI_OPEN_SSID_LONG_QUOTED TEST_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-long-quoted"
|
||||
#define TEST_IFCFG_WIFI_OPEN_SSID_LONG_HEX TEST_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-long-hex"
|
||||
|
|
@ -3298,6 +3436,8 @@ int main (int argc, char **argv)
|
|||
test_read_wifi_wpa_eap_ttls_tls ();
|
||||
test_read_wifi_wep_eap_ttls_chap ();
|
||||
|
||||
test_write_wired_static ();
|
||||
|
||||
basename = g_path_get_basename (argv[0]);
|
||||
fprintf (stdout, "%s: SUCCESS\n", basename);
|
||||
g_free (basename);
|
||||
|
|
|
|||
|
|
@ -523,7 +523,7 @@ write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg)
|
|||
nm_setting_connection_get_autoconnect (s_con) ? "yes" : "no",
|
||||
FALSE);
|
||||
tmp = g_strdup_printf ("%llu", nm_setting_connection_get_timestamp (s_con));
|
||||
svSetValue (ifcfg, "LAST_CONNECTION", tmp, FALSE);
|
||||
svSetValue (ifcfg, "LAST_CONNECT", tmp, FALSE);
|
||||
g_free (tmp);
|
||||
}
|
||||
|
||||
|
|
@ -534,6 +534,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
|
|||
const char *value;
|
||||
char *addr_key, *prefix_key, *gw_key, *tmp;
|
||||
guint32 i, num;
|
||||
GString *searches;
|
||||
|
||||
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
|
||||
if (!s_ip4) {
|
||||
|
|
@ -564,9 +565,9 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
|
|||
prefix_key = g_strdup ("PREFIX");
|
||||
gw_key = g_strdup ("GATEWAY");
|
||||
} else {
|
||||
addr_key = g_strdup_printf ("IPADDR%d", i);
|
||||
prefix_key = g_strdup_printf ("PREFIX%d", i);
|
||||
gw_key = g_strdup_printf ("GATEWAY%d", i);
|
||||
addr_key = g_strdup_printf ("IPADDR%d", i + 1);
|
||||
prefix_key = g_strdup_printf ("PREFIX%d", i + 1);
|
||||
gw_key = g_strdup_printf ("GATEWAY%d", i + 1);
|
||||
}
|
||||
|
||||
if (i >= num) {
|
||||
|
|
@ -604,7 +605,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
|
|||
char buf[INET_ADDRSTRLEN + 1];
|
||||
guint32 ip;
|
||||
|
||||
addr_key = g_strdup_printf ("DNS%d", i);
|
||||
addr_key = g_strdup_printf ("DNS%d", i + 1);
|
||||
|
||||
if (i >= num)
|
||||
svSetValue (ifcfg, addr_key, NULL, FALSE);
|
||||
|
|
@ -618,6 +619,42 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
|
|||
g_free (addr_key);
|
||||
}
|
||||
|
||||
num = nm_setting_ip4_config_get_num_dns_searches (s_ip4);
|
||||
if (num > 0) {
|
||||
searches = g_string_new (NULL);
|
||||
for (i = 0; i < num; i++) {
|
||||
if (i > 0)
|
||||
g_string_append_c (searches, ' ');
|
||||
g_string_append (searches, nm_setting_ip4_config_get_dns_search (s_ip4, i));
|
||||
}
|
||||
svSetValue (ifcfg, "DOMAIN", searches->str, FALSE);
|
||||
g_string_free (searches, TRUE);
|
||||
} else
|
||||
svSetValue (ifcfg, "DOMAIN", NULL, FALSE);
|
||||
|
||||
svSetValue (ifcfg, "PEERDNS", NULL, FALSE);
|
||||
svSetValue (ifcfg, "PEERROUTES", NULL, FALSE);
|
||||
svSetValue (ifcfg, "DHCP_HOSTNAME", NULL, FALSE);
|
||||
svSetValue (ifcfg, "DHCP_CLIENT_ID", NULL, FALSE);
|
||||
if (!strcmp (value, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
|
||||
svSetValue (ifcfg, "PEERDNS",
|
||||
nm_setting_ip4_config_get_ignore_auto_dns (s_ip4) ? "no" : "yes",
|
||||
FALSE);
|
||||
|
||||
svSetValue (ifcfg, "PEERROUTES",
|
||||
nm_setting_ip4_config_get_ignore_auto_routes (s_ip4) ? "no" : "yes",
|
||||
FALSE);
|
||||
|
||||
value = nm_setting_ip4_config_get_dhcp_hostname (s_ip4);
|
||||
if (value)
|
||||
svSetValue (ifcfg, "DHCP_HOSTNAME", value, FALSE);
|
||||
|
||||
value = nm_setting_ip4_config_get_dhcp_client_id (s_ip4);
|
||||
if (value)
|
||||
svSetValue (ifcfg, "DHCP_CLIENT_ID", value, FALSE);
|
||||
}
|
||||
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
@ -641,11 +678,12 @@ escape_id (const char *id)
|
|||
return escaped;
|
||||
}
|
||||
|
||||
gboolean
|
||||
static gboolean
|
||||
write_connection (NMConnection *connection,
|
||||
const char *ifcfg_dir,
|
||||
const char *filename,
|
||||
const char *keyfile,
|
||||
char **out_filename,
|
||||
GError **error)
|
||||
{
|
||||
NMSettingConnection *s_con;
|
||||
|
|
@ -670,7 +708,7 @@ write_connection (NMConnection *connection,
|
|||
char *escaped;
|
||||
|
||||
escaped = escape_id (nm_setting_connection_get_id (s_con));
|
||||
ifcfg_name = g_strdup_printf (IFCFG_DIR "/ifcfg-%s", escaped);
|
||||
ifcfg_name = g_strdup_printf ("%s/ifcfg-%s", ifcfg_dir, escaped);
|
||||
ifcfg = svCreateFile (ifcfg_name);
|
||||
g_free (escaped);
|
||||
}
|
||||
|
|
@ -717,6 +755,11 @@ write_connection (NMConnection *connection,
|
|||
}
|
||||
|
||||
svCloseFile (ifcfg);
|
||||
|
||||
/* Only return the filename if this was a newly written ifcfg */
|
||||
if (out_filename && !filename)
|
||||
*out_filename = g_strdup (ifcfg_name);
|
||||
|
||||
success = TRUE;
|
||||
|
||||
out:
|
||||
|
|
@ -724,3 +767,22 @@ out:
|
|||
return success;
|
||||
}
|
||||
|
||||
gboolean
|
||||
writer_new_connection (NMConnection *connection,
|
||||
const char *ifcfg_dir,
|
||||
char **out_filename,
|
||||
GError **error)
|
||||
{
|
||||
return write_connection (connection, ifcfg_dir, NULL, NULL, out_filename, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
writer_update_connection (NMConnection *connection,
|
||||
const char *ifcfg_dir,
|
||||
const char *filename,
|
||||
const char *keyfile,
|
||||
GError **error)
|
||||
{
|
||||
return write_connection (connection, ifcfg_dir, filename, keyfile, NULL, error);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,10 +25,15 @@
|
|||
#include <glib.h>
|
||||
#include <nm-connection.h>
|
||||
|
||||
gboolean write_connection (NMConnection *connection,
|
||||
const char *ifcfg_dir,
|
||||
const char *filename,
|
||||
const char *keyfile,
|
||||
GError **error);
|
||||
gboolean writer_new_connection (NMConnection *connection,
|
||||
const char *ifcfg_dir,
|
||||
char **out_filename,
|
||||
GError **error);
|
||||
|
||||
gboolean writer_update_connection (NMConnection *connection,
|
||||
const char *ifcfg_dir,
|
||||
const char *filename,
|
||||
const char *keyfile,
|
||||
GError **error);
|
||||
|
||||
#endif /* _WRITER_H_ */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue