Commit 429023b2 authored by Dan Williams's avatar Dan Williams

policy: remove old /etc/hosts cleaning code (bgo #729689)

NetworkManager stopped touching /etc/hosts in late 2010 before the
NetworkManager 0.8.1 release.  The code in nm-policy-hosts.c's only
purpose is to remove any of the entries that NetworkManager added long
ago.

I think we're at the point where people have already upgraded to
NetworkManager 0.8.1 or later and thus this code would be a NOP.  The
only risk is that some stale /etc/hosts entries will be left if you
upgrade from NM 0.8 or lower to anything higher than that.

FWIW, Ubuntu Lucid (10.04) ships NM 0.8.0 and SLES11 ships NM 0.7.0, so
if users of these distros upgraded to a later NetworkManager they might
run into the stale entries issue if we remove this code from NM.  But
given how old these distros are, it seems unlikely that users will do a
direct upgrade to something 4+ years newer...

https://bugzilla.gnome.org/show_bug.cgi?id=729689
parent dd3d5b22
......@@ -184,7 +184,6 @@ valgrind-*.log
/src/tests/test-general
/src/tests/test-ip4-config
/src/tests/test-ip6-config
/src/tests/test-policy-hosts
/src/tests/test-wifi-ap-utils
/src/tests/test-resolvconf-capture
/src/dnsmasq-manager/tests/test-dnsmasq-utils
......
......@@ -232,8 +232,6 @@ nm_sources = \
nm-auth-subject.h \
nm-manager.c \
nm-manager.h \
nm-policy-hosts.c \
nm-policy-hosts.h \
nm-policy.c \
nm-policy.h \
nm-properties-changed-signal.c \
......
......@@ -52,7 +52,6 @@
#include "nm-hostname-provider.h"
#include "nm-vpn-manager.h"
#include "nm-logging.h"
#include "nm-policy-hosts.h"
#include "nm-config.h"
#include "nm-posix-signals.h"
#include "nm-session-monitor.h"
......@@ -651,9 +650,6 @@ main (int argc, char *argv[])
}
}
/* Clean leftover "# Added by NetworkManager" entries from /etc/hosts */
nm_policy_hosts_clean_etc_hosts ();
nm_manager_start (manager);
/* Make sure the loopback interface is up. If interface is down, we bring
......
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* 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) 2004 - 2010 Red Hat, Inc.
*/
#include <config.h>
#include <string.h>
#include "nm-policy-hosts.h"
#include "nm-logging.h"
#define ADDED_TAG "# Added by NetworkManager"
GString *
nm_policy_get_etc_hosts (const char *contents, gsize contents_len)
{
char **lines = NULL, **iter;
GString *new_contents = NULL;
if (contents_len == 0 || !strstr (contents, ADDED_TAG))
return NULL;
new_contents = g_string_sized_new (contents_len);
/* Remove "# Added ..." lines */
lines = g_strsplit_set (contents, "\n\r", -1);
for (iter = lines; iter && *iter; iter++) {
if (!strstr (*iter, ADDED_TAG)) {
g_string_append (new_contents, *iter);
g_string_append_c (new_contents, '\n');
}
}
g_strfreev (lines);
/* Remove last blank line at end of file, if one exists; this is
* an artifact of how g_strsplit_set() works.
*/
if ( (new_contents->len > 2)
&& (new_contents->str[new_contents->len - 1] == '\n'))
g_string_truncate (new_contents, new_contents->len - 1);
return new_contents;
}
/* remove any leftover "# Added by NetworkManager" lines */
void
nm_policy_hosts_clean_etc_hosts (void)
{
char *contents = NULL;
gsize contents_len = 0;
GError *error = NULL;
GString *new;
if (!g_file_get_contents (SYSCONFDIR "/hosts", &contents, &contents_len, &error)) {
nm_log_dbg (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s",
error ? error->code : 0,
(error && error->message) ? error->message : "(unknown)");
g_clear_error (&error);
return;
}
new = nm_policy_get_etc_hosts (contents, contents_len);
g_free (contents);
if (new && new->len) {
nm_log_dbg (LOGD_DNS, "Cleaning leftovers from /etc/hosts");
g_clear_error (&error);
if (!g_file_set_contents (SYSCONFDIR "/hosts", new->str, -1, &error)) {
nm_log_dbg (LOGD_DNS, "couldn't update " SYSCONFDIR "/hosts: (%d) %s",
error ? error->code : 0,
(error && error->message) ? error->message : "(unknown)");
g_clear_error (&error);
}
}
if (new)
g_string_free (new, TRUE);
}
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* 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) 2004 - 2010 Red Hat, Inc.
*/
#ifndef NM_POLICY_HOSTS_H
#define NM_POLICT_HOSTS_H
#include <glib.h>
void nm_policy_hosts_clean_etc_hosts (void);
/* Only for testcases; don't use outside of nm-policy-hosts.c */
GString *nm_policy_get_etc_hosts (const char *contents, gsize contents_len);
#endif /* NM_POLICY_HOSTS_H */
......@@ -16,7 +16,6 @@ AM_CPPFLAGS = \
noinst_PROGRAMS = \
test-dhcp-options \
test-general \
test-policy-hosts \
test-wifi-ap-utils \
test-ip4-config \
test-ip6-config \
......@@ -36,14 +35,6 @@ test_dhcp_options_CPPFLAGS = \
test_dhcp_options_LDADD = \
$(top_builddir)/src/libNetworkManager.la
####### policy /etc/hosts test #######
test_policy_hosts_SOURCES = \
test-policy-hosts.c
test_policy_hosts_LDADD = \
$(top_builddir)/src/libNetworkManager.la
####### wifi ap utils test #######
test_wifi_ap_utils_SOURCES = \
......@@ -98,5 +89,5 @@ EXTRA_DIST = test-secret-agent.py
###########################################
TESTS = test-dhcp-options test-policy-hosts test-wifi-ap-utils test-ip4-config test-ip6-config test-dcb test-resolvconf-capture test-general
TESTS = test-dhcp-options test-wifi-ap-utils test-ip4-config test-ip6-config test-dcb test-resolvconf-capture test-general
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* 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, 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) 2010 Red Hat, Inc.
*
*/
#include <glib.h>
#include <string.h>
#include "nm-policy-hosts.h"
#define DEBUG 0
static void
test_generic (const char *before, const char *after)
{
GString *newc;
/* Get the new /etc/hosts contents */
newc = nm_policy_get_etc_hosts (before, strlen (before));
if (after == NULL) {
/* No change to /etc/hosts required */
#if DEBUG
if (newc != NULL) {
g_message ("\n- NEW ---------------------------------\n"
"%s"
"+ EXPECTED NONE +++++++++++++++++++++++++\n",
newc->str);
}
#endif
g_assert (newc == NULL);
} else {
g_assert (newc != NULL);
#if DEBUG
g_message ("\n- NEW ---------------------------------\n"
"%s"
"+ EXPECTED ++++++++++++++++++++++++++++++\n"
"%s"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
newc->str, after);
#endif
g_assert (strcmp (newc->str, after) == 0);
g_string_free (newc, TRUE);
}
}
/*******************************************/
static const char *generic_before = \
"# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n";
static void
test_hosts_generic (void)
{
test_generic (generic_before, NULL);
}
/*******************************************/
static const char *generic_no_boilerplate_before = \
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"127.0.0.1 lcmd.us.intellitxt.com\n";
static void
test_hosts_generic_no_boilerplate (void)
{
test_generic (generic_no_boilerplate_before, NULL);
}
/*******************************************/
static const char *leftover_before = \
"# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n"
"192.168.1.2 comet # Added by NetworkManager\n"
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"192.168.1.3 comet\n"
"3001:abba::3234 comet\n"
"\n"
"127.0.0.1 lcmd.us.intellitxt.com\n";
static const char *leftover_after = \
"# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"192.168.1.3 comet\n"
"3001:abba::3234 comet\n"
"\n"
"127.0.0.1 lcmd.us.intellitxt.com\n";
static void
test_hosts_leftover (void)
{
test_generic (leftover_before, leftover_after);
}
/*******************************************/
static const char *leftover_double_newline_before = \
"# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n"
"192.168.1.2 comet # Added by NetworkManager\n"
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"192.168.1.3 comet\n"
"3001:abba::3234 comet\n"
"\n"
"127.0.0.1 lcmd.us.intellitxt.com\n"
"\n";
static const char *leftover_double_newline_after = \
"# Do not remove the following line, or various programs\n"
"# that require network functionality will fail.\n"
"127.0.0.1 localhost.localdomain localhost\n"
"::1 localhost6.localdomain6 localhost6\n"
"192.168.1.3 comet\n"
"3001:abba::3234 comet\n"
"\n"
"127.0.0.1 lcmd.us.intellitxt.com\n"
"\n";
static void
test_hosts_leftover_double_newline (void)
{
test_generic (leftover_double_newline_before, leftover_double_newline_after);
}
/*******************************************/
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/hosts/generic", test_hosts_generic);
g_test_add_func ("/hosts/generic_no_boilerplate", test_hosts_generic_no_boilerplate);
g_test_add_func ("/hosts/leftover", test_hosts_leftover);
g_test_add_func ("/hosts/leftover_double_newline", test_hosts_leftover_double_newline);
return g_test_run ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment