diff options
| author | Michael Biebl <biebl@debian.org> | 2010-02-08 14:47:47 +0000 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2010-02-08 14:47:47 +0000 |
| commit | c7a5f91abbea2a0b09a03503e972eee7b1f3e008 (patch) | |
| tree | 7173a23541a03be367da1612ae3eeff19dfc83bc | |
| parent | 3b72b1eb327563ab2eb89857c439476dd9aa4a1e (diff) | |
* debian/patches/04-etc-hosts-rewrite.patch
- Be more selective when adding hostname to /etc/hosts.
Patch pulled from upstream Git. (Closes: #567411)
git-svn-id: svn+ssh://svn.debian.org/svn/pkg-utopia/packages/unstable/networkmanager@3506 ceb527fc-18e6-0310-9fe2-813c157c29e7
| -rw-r--r-- | debian/changelog | 3 | ||||
| -rw-r--r-- | debian/patches/04-etc-hosts-rewrite.patch | 172 | ||||
| -rw-r--r-- | debian/patches/series | 1 |
3 files changed, 176 insertions, 0 deletions
diff --git a/debian/changelog b/debian/changelog index f716666d..b9663f54 100644 --- a/debian/changelog +++ b/debian/changelog @@ -5,6 +5,9 @@ network-manager (0.7.999-3) UNRELEASED; urgency=low plugins provides a hostname, fallback to the hostname that was set when NetworkManager was started. Patch pulled from upstream Git. (Closes: #567207) + * debian/patches/04-etc-hosts-rewrite.patch + - Be more selective when adding hostname to /etc/hosts. + Patch pulled from upstream Git. (Closes: #567411) -- Michael Biebl <biebl@debian.org> Mon, 08 Feb 2010 15:35:45 +0100 diff --git a/debian/patches/04-etc-hosts-rewrite.patch b/debian/patches/04-etc-hosts-rewrite.patch new file mode 100644 index 00000000..3e640f69 --- /dev/null +++ b/debian/patches/04-etc-hosts-rewrite.patch @@ -0,0 +1,172 @@ +commit f6c383ff14412ea264fa8740dec84f92a89bd385 +Author: Dan Williams <dcbw@redhat.com> +Date: Mon Feb 1 17:29:58 2010 -0800 + + policy: be more selective when adding hostname to /etc/hosts (debian #567411) + + If the user has already mapped the hostname to some other address, + assume the user knows what they are doing (since they probably do) + and leave that mapping alone, and create a minimal 127.0.0.1 entry. + If there was no hostname mapping, add it to the 127.0.0.1 entry + like NM did before. + +diff --git a/src/NetworkManagerPolicy.c b/src/NetworkManagerPolicy.c +index 7034520..1b7801c 100644 +--- a/src/NetworkManagerPolicy.c ++++ b/src/NetworkManagerPolicy.c +@@ -252,13 +252,84 @@ get_best_device (NMManager *manager, NMActRequest **out_req) + return best; + } + ++static gboolean ++is_localhost_mapping (const char *str) ++{ ++ return (!strncmp (str, "127.0.0.1", strlen ("127.0.0.1")) && strstr (str, "localhost")); ++} ++ ++static gboolean ++find_token (const char *line, const char *token) ++{ ++ const char *start = line, *p = line; ++ ++ g_return_val_if_fail (line != NULL, FALSE); ++ g_return_val_if_fail (token != NULL, FALSE); ++ g_return_val_if_fail (strlen (token) > 0, FALSE); ++ ++ /* Walk through the line to find the next whitespace character */ ++ while (p <= line + strlen (line)) { ++ if (isblank (*p) || (*p == '\0')) { ++ /* Token starts with 'start' and ends with 'end' */ ++ if ((p > start) && *start && !strncmp (start, token, (p - start))) ++ return TRUE; /* found */ ++ ++ /* not found; advance start and continue looking */ ++ start = p + 1; ++ } ++ p++; ++ } ++ ++ return FALSE; ++} ++ ++#if 0 ++/* Testcase for find_token; break it out and add it to the testsuite */ ++ ++typedef struct { ++ const char *line; ++ const char *token; ++ gboolean expected; ++} Foo; ++ ++static Foo foo[] = { ++ { "127.0.0.1\tfoobar\tblah", "blah", TRUE }, ++ { "", "blah", FALSE }, ++ { "1.1.1.1\tbork\tfoo", "blah", FALSE }, ++ { "127.0.0.1 foobar\tblah", "blah", TRUE }, ++ { "127.0.0.1 foobar blah", "blah", TRUE }, ++ { "192.168.1.1 blah borkbork", "blah", TRUE }, ++ { "192.168.1.1 foobar\tblah borkbork", "blah", TRUE }, ++ { "192.168.1.1\tfoobar\tblah\tborkbork", "blah", TRUE }, ++ { "192.168.1.1 \tfoobar \tblah \tborkbork\t ", "blah", TRUE }, ++ { "\t\t\t\t \t\t\tasdfadf a\t\t\t\t\t \t\t\t\t\t ", "blah", FALSE }, ++ { NULL, NULL, FALSE } ++}; ++ ++int main(int argc, char **argv) ++{ ++ Foo *iter = &foo[0]; ++ ++ while (iter->line) { ++ if (find_token (iter->line, iter->token) != iter->expected) { ++ g_message ("Failed: '%s' <= '%s' (%d)", iter->line, iter->token, iter->expected); ++ return 1; ++ } ++ iter++; ++ } ++ ++ g_message ("Success"); ++ return 0; ++} ++#endif ++ + #define FALLBACK_HOSTNAME "localhost.localdomain" + + static gboolean + update_etc_hosts (const char *hostname) + { + char *contents = NULL; +- char **lines = NULL, **line; ++ char **lines = NULL, **line, **host_mapping = NULL; + GError *error = NULL; + gboolean initial_comments = TRUE; + gboolean added = FALSE; +@@ -285,25 +356,62 @@ update_etc_hosts (const char *hostname) + return FALSE; + } + +- /* Replace any 127.0.0.1 entry that is at the beginning of the file or right +- * after initial comments. If there is no 127.0.0.1 entry at the beginning +- * or after initial comments, add one there and ignore any other 127.0.0.1 +- * entries. ++ /* Two-pass modification of /etc/hosts: ++ * ++ * 1) Look for a non-comment, non-localhost line that contains the current ++ * hostname. Mark that line. ++ * ++ * 2) For each line in the existing /etc/hosts, add it to the new /etc/hosts ++ * unless it starts with 127.0.0.1 and is right after the initial comments ++ * (if any) and contains "localhost". + */ ++ ++ /* Find any existing hostname mapping */ + for (line = lines; lines && *line; line++) { ++ /* Look for any line that (a) contains the current hostname, and ++ * (b) does not start with '127.0.0.1' and contain 'localhost'. ++ */ ++ if ( strlen (*line) ++ && (*line[0] != '#') ++ && find_token (*line, hostname) ++ && !is_localhost_mapping (*line)) { ++ host_mapping = line; ++ break; ++ } ++ } ++ ++ /* Construct the new hosts file; replace any 127.0.0.1 entry that is at the ++ * beginning of the file or right after initial comments and contains ++ * the string 'localhost'. If there is no 127.0.0.1 entry at the beginning ++ * or after initial comments that contains 'localhost', add one there ++ * and ignore any other 127.0.0.1 entries that contain 'localhost'. ++ */ ++ for (line = lines, initial_comments = TRUE; lines && *line; line++) { + gboolean add_line = TRUE; + + /* This is the first line after the initial comments */ +- if (initial_comments && (*line[0] != '#')) { ++ if (strlen (*line) && initial_comments && (*line[0] != '#')) { + initial_comments = FALSE; +- g_string_append_printf (new_contents, "127.0.0.1\t%s", hostname); ++ ++ /* If some other line contained the hostname, make a simple ++ * localhost mapping and assume the user knows what they are doing ++ * with their manual hostname entry. Otherwise if the hostname ++ * wasn't found somewhere else, add it to the localhost mapping line ++ * to make sure it's mapped to something. ++ */ ++ if (host_mapping) ++ g_string_append (new_contents, "127.0.0.1"); ++ else ++ g_string_append_printf (new_contents, "127.0.0.1\t%s", hostname); ++ + if (strcmp (hostname, FALLBACK_HOSTNAME)) + g_string_append_printf (new_contents, "\t" FALLBACK_HOSTNAME); ++ + g_string_append (new_contents, "\tlocalhost\n"); + added = TRUE; + + /* Don't add the entry if it's supposed to be the actual localhost reverse mapping */ +- if (!strncmp (*line, "127.0.0.1", strlen ("127.0.0.1")) && strstr (*line, "localhost")) ++ if (is_localhost_mapping (*line)) + add_line = FALSE; + } + diff --git a/debian/patches/series b/debian/patches/series index 060d8cd0..08786385 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -1,3 +1,4 @@ 02-dbus_access_network_manager.patch 03-hostname-fallback.patch +04-etc-hosts-rewrite.patch #20-manual_means_always_online.patch |