summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2015-05-03 22:54:55 +0200
committerBeniamino Galvani <bgalvani@redhat.com>2015-05-13 17:15:35 +0200
commita1d76a019e287e8d6e4f962d214c6f01a1446780 (patch)
treeda18a439378169e4430a94a642facf755d0db7e5
parent8d9776997b48e66709cf717eb8fd630869661ad9 (diff)
downloadNetworkManager-a1d76a019e287e8d6e4f962d214c6f01a1446780.tar.gz
core: capture DNS options from resolv.conf
-rw-r--r--src/NetworkManagerUtils.c50
-rw-r--r--src/NetworkManagerUtils.h1
-rw-r--r--src/nm-ip4-config.c25
-rw-r--r--src/nm-ip4-config.h2
-rw-r--r--src/nm-ip6-config.c27
-rw-r--r--src/nm-ip6-config.h1
-rw-r--r--src/tests/test-resolvconf-capture.c102
7 files changed, 192 insertions, 16 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c
index 368ceb3438..afcb88b84b 100644
--- a/src/NetworkManagerUtils.c
+++ b/src/NetworkManagerUtils.c
@@ -1473,6 +1473,56 @@ nm_utils_read_resolv_conf_nameservers (const char *rc_contents)
return nameservers;
}
+/**
+ * nm_utils_read_resolv_conf_dns_options():
+ * @rc_contents: contents of a resolv.conf; or %NULL to read /etc/resolv.conf
+ *
+ * Reads all dns options out of @rc_contents or /etc/resolv.conf and returns
+ * them.
+ *
+ * Returns: a #GPtrArray of 'char *' elements of each option
+ */
+GPtrArray *
+nm_utils_read_resolv_conf_dns_options (const char *rc_contents)
+{
+ GPtrArray *options = NULL;
+ char *contents = NULL;
+ char **lines, **line_iter;
+ char **tokens, **token_iter;
+ char *p;
+
+ if (rc_contents)
+ contents = g_strdup (rc_contents);
+ else {
+ if (!g_file_get_contents (_PATH_RESCONF, &contents, NULL, NULL))
+ return NULL;
+ }
+
+ options = g_ptr_array_new_full (3, g_free);
+
+ lines = g_strsplit_set (contents, "\r\n", -1);
+ for (line_iter = lines; *line_iter; line_iter++) {
+ if (!g_str_has_prefix (*line_iter, "options"))
+ continue;
+ p = *line_iter + strlen ("options");
+ if (!g_ascii_isspace (*p++))
+ continue;
+
+ tokens = g_strsplit (p, " ", 0);
+ for (token_iter = tokens; token_iter && *token_iter; token_iter++) {
+ g_strstrip (*token_iter);
+ if (!*token_iter[0])
+ continue;
+ g_ptr_array_add (options, g_strdup (*token_iter));
+ }
+ g_strfreev (tokens);
+ }
+ g_strfreev (lines);
+ g_free (contents);
+
+ return options;
+}
+
static GHashTable *
check_property_in_hash (GHashTable *hash,
const char *s_name,
diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h
index 1e62ee7b62..fe88a7201b 100644
--- a/src/NetworkManagerUtils.h
+++ b/src/NetworkManagerUtils.h
@@ -123,6 +123,7 @@ void nm_utils_complete_generic (NMConnection *connection,
char *nm_utils_new_vlan_name (const char *parent_iface, guint32 vlan_id);
GPtrArray *nm_utils_read_resolv_conf_nameservers (const char *rc_contents);
+GPtrArray *nm_utils_read_resolv_conf_dns_options (const char *rc_contents);
typedef gboolean (NMUtilsMatchFilterFunc) (NMConnection *connection, gpointer user_data);
diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c
index 7092f75c3d..e893351f6f 100644
--- a/src/nm-ip4-config.c
+++ b/src/nm-ip4-config.c
@@ -34,6 +34,7 @@
#include "NetworkManagerUtils.h"
#include "nm-core-internal.h"
#include "nm-route-manager.h"
+#include "nm-utils-private.h"
G_DEFINE_TYPE (NMIP4Config, nm_ip4_config, G_TYPE_OBJECT)
@@ -139,9 +140,10 @@ same_prefix (guint32 address1, guint32 address2, int plen)
*/
gboolean
nm_ip4_config_capture_resolv_conf (GArray *nameservers,
+ GPtrArray *dns_options,
const char *rc_contents)
{
- GPtrArray *read_ns;
+ GPtrArray *read_ns, *read_options;
guint i, j;
gboolean changed = FALSE;
@@ -169,8 +171,25 @@ nm_ip4_config_capture_resolv_conf (GArray *nameservers,
changed = TRUE;
}
}
-
g_ptr_array_unref (read_ns);
+
+ if (dns_options) {
+ read_options = nm_utils_read_resolv_conf_dns_options (rc_contents);
+ if (!read_options)
+ return changed;
+
+ for (i = 0; i < read_options->len; i++) {
+ const char *s = g_ptr_array_index (read_options, i);
+
+ if (_nm_utils_dns_option_validate (s, NULL, NULL, FALSE, dns_option_descs) &&
+ _nm_utils_dns_option_find_idx (dns_options, s) < 0) {
+ g_ptr_array_add (dns_options, g_strdup (s));
+ changed = TRUE;
+ }
+ }
+ g_ptr_array_unref (read_options);
+ }
+
return changed;
}
@@ -247,7 +266,7 @@ nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf)
* nameservers from /etc/resolv.conf.
*/
if (priv->addresses->len && has_gateway && capture_resolv_conf) {
- if (nm_ip4_config_capture_resolv_conf (priv->nameservers, NULL))
+ if (nm_ip4_config_capture_resolv_conf (priv->nameservers, priv->dns_options, NULL))
_NOTIFY (config, PROP_NAMESERVERS);
}
diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h
index 9b74e8102b..a69af7e795 100644
--- a/src/nm-ip4-config.h
+++ b/src/nm-ip4-config.h
@@ -163,7 +163,7 @@ gboolean nm_ip4_config_equal (const NMIP4Config *a, const NMIP4Config *b);
/******************************************************/
/* Testing-only functions */
-gboolean nm_ip4_config_capture_resolv_conf (GArray *nameservers,
+gboolean nm_ip4_config_capture_resolv_conf (GArray *nameservers, GPtrArray *dns_options,
const char *rc_contents);
#endif /* __NETWORKMANAGER_IP4_CONFIG_H__ */
diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c
index 5716af20c9..df2952717c 100644
--- a/src/nm-ip6-config.c
+++ b/src/nm-ip6-config.c
@@ -33,6 +33,7 @@
#include "nm-dbus-glib-types.h"
#include "nm-ip6-config-glue.h"
#include "nm-route-manager.h"
+#include "nm-utils-private.h"
#include "NetworkManagerUtils.h"
G_DEFINE_TYPE (NMIP6Config, nm_ip6_config, G_TYPE_OBJECT)
@@ -140,9 +141,10 @@ same_prefix (const struct in6_addr *address1, const struct in6_addr *address2, i
*/
gboolean
nm_ip6_config_capture_resolv_conf (GArray *nameservers,
+ GPtrArray *dns_options,
const char *rc_contents)
{
- GPtrArray *read_ns;
+ GPtrArray *read_ns, *read_options;
guint i, j;
gboolean changed = FALSE;
@@ -172,8 +174,25 @@ nm_ip6_config_capture_resolv_conf (GArray *nameservers,
changed = TRUE;
}
}
-
g_ptr_array_unref (read_ns);
+
+ if (dns_options) {
+ read_options = nm_utils_read_resolv_conf_dns_options (rc_contents);
+ if (!read_options)
+ return changed;
+
+ for (i = 0; i < read_options->len; i++) {
+ const char *s = g_ptr_array_index (read_options, i);
+
+ if (_nm_utils_dns_option_validate (s, NULL, NULL, TRUE, dns_option_descs) &&
+ _nm_utils_dns_option_find_idx (dns_options, s) < 0) {
+ g_ptr_array_add (dns_options, g_strdup (s));
+ changed = TRUE;
+ }
+ }
+ g_ptr_array_unref (read_options);
+ }
+
return changed;
}
@@ -361,7 +380,9 @@ nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6Co
* nameservers from /etc/resolv.conf.
*/
if (priv->addresses->len && has_gateway && capture_resolv_conf)
- notify_nameservers = nm_ip6_config_capture_resolv_conf (priv->nameservers, NULL);
+ notify_nameservers = nm_ip6_config_capture_resolv_conf (priv->nameservers,
+ priv->dns_options,
+ NULL);
g_array_sort_with_data (priv->addresses, _addresses_sort_cmp, GINT_TO_POINTER (use_temporary));
diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h
index 28513c2174..bf64fc26ac 100644
--- a/src/nm-ip6-config.h
+++ b/src/nm-ip6-config.h
@@ -145,6 +145,7 @@ gboolean nm_ip6_config_equal (const NMIP6Config *a, const NMIP6Config *b);
/* Testing-only functions */
gboolean nm_ip6_config_capture_resolv_conf (GArray *nameservers,
+ GPtrArray *dns_options,
const char *rc_contents);
#endif /* __NETWORKMANAGER_IP6_CONFIG_H__ */
diff --git a/src/tests/test-resolvconf-capture.c b/src/tests/test-resolvconf-capture.c
index f5cb298b9f..bf0fff0b1d 100644
--- a/src/tests/test-resolvconf-capture.c
+++ b/src/tests/test-resolvconf-capture.c
@@ -35,10 +35,10 @@ test_capture_empty (void)
GArray *ns4 = g_array_new (FALSE, FALSE, sizeof (guint32));
GArray *ns6 = g_array_new (FALSE, FALSE, sizeof (struct in6_addr));
- g_assert (nm_ip4_config_capture_resolv_conf (ns4, "") == FALSE);
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, "") == FALSE);
g_assert_cmpint (ns4->len, ==, 0);
- g_assert (nm_ip6_config_capture_resolv_conf (ns6, "") == FALSE);
+ g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, "") == FALSE);
g_assert_cmpint (ns6->len, ==, 0);
g_array_free (ns4, TRUE);
@@ -67,6 +67,12 @@ assert_dns6_entry (const GArray *a, guint i, const char *s)
}
static void
+assert_dns_option (GPtrArray *a, guint i, const char *s)
+{
+ g_assert_cmpstr (a->pdata[i], ==, s);
+}
+
+static void
test_capture_basic4 (void)
{
GArray *ns4 = g_array_new (FALSE, FALSE, sizeof (guint32));
@@ -77,7 +83,7 @@ test_capture_basic4 (void)
"nameserver 4.2.2.1\r\n"
"nameserver 4.2.2.2\r\n";
- g_assert (nm_ip4_config_capture_resolv_conf (ns4, rc));
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc));
g_assert_cmpint (ns4->len, ==, 2);
assert_dns4_entry (ns4, 0, "4.2.2.1");
assert_dns4_entry (ns4, 1, "4.2.2.2");
@@ -98,7 +104,7 @@ test_capture_dup4 (void)
"nameserver 4.2.2.2\r\n";
/* Check that duplicates are ignored */
- g_assert (nm_ip4_config_capture_resolv_conf (ns4, rc));
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc));
g_assert_cmpint (ns4->len, ==, 2);
assert_dns4_entry (ns4, 0, "4.2.2.1");
assert_dns4_entry (ns4, 1, "4.2.2.2");
@@ -117,7 +123,7 @@ test_capture_basic6 (void)
"nameserver 2001:4860:4860::8888\r\n"
"nameserver 2001:4860:4860::8844\r\n";
- g_assert (nm_ip6_config_capture_resolv_conf (ns6, rc));
+ g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, rc));
g_assert_cmpint (ns6->len, ==, 2);
assert_dns6_entry (ns6, 0, "2001:4860:4860::8888");
assert_dns6_entry (ns6, 1, "2001:4860:4860::8844");
@@ -138,7 +144,7 @@ test_capture_dup6 (void)
"nameserver 2001:4860:4860::8844\r\n";
/* Check that duplicates are ignored */
- g_assert (nm_ip6_config_capture_resolv_conf (ns6, rc));
+ g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, rc));
g_assert_cmpint (ns6->len, ==, 2);
assert_dns6_entry (ns6, 0, "2001:4860:4860::8888");
assert_dns6_entry (ns6, 1, "2001:4860:4860::8844");
@@ -158,7 +164,7 @@ test_capture_addr4_with_6 (void)
"nameserver 4.2.2.2\r\n"
"nameserver 2001:4860:4860::8888\r\n";
- g_assert (nm_ip4_config_capture_resolv_conf (ns4, rc));
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc));
g_assert_cmpint (ns4->len, ==, 2);
assert_dns4_entry (ns4, 0, "4.2.2.1");
assert_dns4_entry (ns4, 1, "4.2.2.2");
@@ -178,7 +184,7 @@ test_capture_addr6_with_4 (void)
"nameserver 2001:4860:4860::8888\r\n"
"nameserver 2001:4860:4860::8844\r\n";
- g_assert (nm_ip6_config_capture_resolv_conf (ns6, rc));
+ g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, rc));
g_assert_cmpint (ns6->len, ==, 2);
assert_dns6_entry (ns6, 0, "2001:4860:4860::8888");
assert_dns6_entry (ns6, 1, "2001:4860:4860::8844");
@@ -197,7 +203,7 @@ test_capture_format (void)
"nameserver\t\t4.2.2.4\r\n" /* good */
"nameserver 4.2.2.5\t\t\r\n"; /* good */
- g_assert (nm_ip4_config_capture_resolv_conf (ns4, rc));
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc));
g_assert_cmpint (ns4->len, ==, 3);
assert_dns4_entry (ns4, 0, "4.2.2.3");
assert_dns4_entry (ns4, 1, "4.2.2.4");
@@ -206,6 +212,80 @@ test_capture_format (void)
g_array_free (ns4, TRUE);
}
+static void
+test_capture_dns_options (void)
+{
+ GArray *ns4 = g_array_new (FALSE, FALSE, sizeof (guint32));
+ GPtrArray *dns_options = g_ptr_array_new_with_free_func (g_free);
+ const char *rc =
+"nameserver 4.2.2.1\r\n"
+"options debug rotate timeout:5 \r\n"
+"options edns0\r\n";
+
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, dns_options, rc));
+ g_assert_cmpint (dns_options->len, ==, 4);
+ assert_dns_option (dns_options, 0, "debug");
+ assert_dns_option (dns_options, 1, "rotate");
+ assert_dns_option (dns_options, 2, "timeout:5");
+ assert_dns_option (dns_options, 3, "edns0");
+
+ g_array_free (ns4, TRUE);
+ g_ptr_array_free (dns_options, TRUE);
+}
+
+static void
+test_capture_dns_options_dup (void)
+{
+ GArray *ns4 = g_array_new (FALSE, FALSE, sizeof (guint32));
+ GPtrArray *dns_options = g_ptr_array_new_with_free_func (g_free);
+ const char *rc =
+"options debug rotate timeout:3\r\n"
+"options edns0 debug\r\n"
+"options timeout:5\r\n";
+
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, dns_options, rc));
+ g_assert_cmpint (dns_options->len, ==, 4);
+ assert_dns_option (dns_options, 0, "debug");
+ assert_dns_option (dns_options, 1, "rotate");
+ assert_dns_option (dns_options, 2, "timeout:3");
+ assert_dns_option (dns_options, 3, "edns0");
+
+ g_array_free (ns4, TRUE);
+ g_ptr_array_free (dns_options, TRUE);
+}
+
+static void
+test_capture_dns_options_valid4 (void)
+{
+ GArray *ns4 = g_array_new (FALSE, FALSE, sizeof (guint32));
+ GPtrArray *dns_options = g_ptr_array_new_with_free_func (g_free);
+ const char *rc =
+"options debug: rotate:yes edns0 foobar : inet6\r\n";
+
+ g_assert (nm_ip4_config_capture_resolv_conf (ns4, dns_options, rc));
+ g_assert_cmpint (dns_options->len, ==, 1);
+ assert_dns_option (dns_options, 0, "edns0");
+
+ g_array_free (ns4, TRUE);
+ g_ptr_array_free (dns_options, TRUE);
+}
+
+static void
+test_capture_dns_options_valid6 (void)
+{
+ GArray *ns6 = g_array_new (FALSE, FALSE, sizeof (guint32));
+ GPtrArray *dns_options = g_ptr_array_new_with_free_func (g_free);
+ const char *rc =
+"options inet6 debug foobar rotate:\r\n";
+
+ g_assert (nm_ip6_config_capture_resolv_conf (ns6, dns_options, rc));
+ g_assert_cmpint (dns_options->len, ==, 2);
+ assert_dns_option (dns_options, 0, "inet6");
+ assert_dns_option (dns_options, 1, "debug");
+
+ g_array_free (ns6, TRUE);
+ g_ptr_array_free (dns_options, TRUE);
+}
/*******************************************/
int
@@ -225,6 +305,10 @@ main (int argc, char **argv)
g_test_add_func ("/resolvconf-capture/addr4-with-6", test_capture_addr4_with_6);
g_test_add_func ("/resolvconf-capture/addr6-with-4", test_capture_addr6_with_4);
g_test_add_func ("/resolvconf-capture/format", test_capture_format);
+ g_test_add_func ("/resolvconf-capture/dns-options", test_capture_dns_options);
+ g_test_add_func ("/resolvconf-capture/dns-options-dup", test_capture_dns_options_dup);
+ g_test_add_func ("/resolvconf-capture/dns-options-valid4", test_capture_dns_options_valid4);
+ g_test_add_func ("/resolvconf-capture/dns-options-valid6", test_capture_dns_options_valid6);
return g_test_run ();
}