core: capture DNS options from resolv.conf

This commit is contained in:
Beniamino Galvani
2015-05-03 22:54:55 +02:00
parent 8d9776997b
commit a1d76a019e
7 changed files with 192 additions and 16 deletions

View File

@@ -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,

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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__ */

View File

@@ -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));

View File

@@ -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__ */

View File

@@ -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);
@@ -66,6 +66,12 @@ assert_dns6_entry (const GArray *a, guint i, const char *s)
g_assert (IN6_ARE_ADDR_EQUAL (&n, m));
}
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)
{
@@ -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 ();
}