about summary refs log tree commit diff
path: root/src/nm-policy-hosts.c
blob: 7723c99759eef490b802ad2c0c01e35bdd451958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- 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 <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <ctype.h>

#include "nm-policy-hosts.h"
#include "nm-logging.h"

gboolean
nm_policy_hosts_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 && (p - start == strlen (token)) && !strncmp (start, token, (p - start)))
				return TRUE; /* found */

			/* not found; advance start and continue looking */
			start = p + 1;
		}
		p++;
	}

	return FALSE;
}

static gboolean
is_local_mapping (const char *str, const char *hostname)
{
	return (   !strncmp (str, "127.0.0.1", strlen ("127.0.0.1"))
	        && nm_policy_hosts_find_token (str, hostname ? hostname : "localhost"));
}

GString *
nm_policy_get_etc_hosts (const char **lines,
                         gsize existing_len,
                         const char *hostname,
                         const char *fallback_hostname,
                         GError **error)
{
	GString *contents = NULL;
	const char **line;
	gboolean found_host_nonlocal = FALSE;
	gboolean found_host = FALSE;
	gboolean found_localhost = FALSE;
	gboolean initial_comments = TRUE;
	gboolean added = FALSE;

	g_return_val_if_fail (lines != NULL, FALSE);
	g_return_val_if_fail (hostname != NULL, FALSE);

	/* /etc/hosts needs at least two things:
	 *
	 * 1) current hostname mapped to any address
	 * 2) 'localhost' mapped to 127.0.0.1
	 *
	 * If both these conditions exist in /etc/hosts, we don't need to bother
	 * updating the file.
	 */

	/* Look for the two cases from above */
	for (line = lines; lines && *line; line++) {
		if (strlen (*line) && (*line[0] != '#')) {
			if (nm_policy_hosts_find_token (*line, hostname)) {
				if (!is_local_mapping (*line, "localhost")) {
					/* hostname is not on a 127.0.0.1 line or the line does not
					 * contain 'localhost'.
					 */
					found_host_nonlocal = TRUE;
				}
				found_host = TRUE;
			}

			if (is_local_mapping (*line, "localhost")) {
				/* a 127.0.0.1 line containing 'localhost' */
				found_localhost = TRUE;
			}
		}

		if (found_localhost && found_host)
			return NULL;  /* No update required */
	}

	contents = g_string_sized_new (existing_len ? existing_len + 100 : 200);
	if (!contents) {
		g_set_error_literal (error, 0, 0, "not enough memory");
		return NULL;
	}

	/* 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 (strlen (*line) && initial_comments && (*line[0] != '#')) {
			initial_comments = FALSE;

			/* If some other line contained the hostname but not 'localhost',
			 * 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 (found_host_nonlocal)
				g_string_append (contents, "127.0.0.1");
			else
				g_string_append_printf (contents, "127.0.0.1\t%s", hostname);

			if (strcmp (hostname, fallback_hostname)) {
				g_string_append_printf (contents, "\t%s", fallback_hostname);
				/* Don't add a standalone 'localhost.localdomain' 127 mapping */
				if (is_local_mapping (*line, fallback_hostname))
					add_line = FALSE;
			}

			g_string_append (contents, "\tlocalhost\n");
			added = TRUE;

			/* Don't add the original line if it is a 'localhost' mapping */
			if (is_local_mapping (*line, "localhost"))
				add_line = FALSE;
		}

		if (add_line) {
			g_string_append (contents, *line);
			/* Only append the new line if this isn't the last line in the file */
			if (*(line+1))
				g_string_append_c (contents, '\n');
		}
	}

	/* Hmm, /etc/hosts was empty for some reason */
	if (!added) {
		g_string_append (contents, "# Do not remove the following line, or various programs\n");
		g_string_append (contents, "# that require network functionality will fail.\n");
		g_string_append_printf (contents, "127.0.0.1\t%s\tlocalhost\n", fallback_hostname);
	}

	return contents;
}

gboolean
nm_policy_hosts_update_etc_hosts (const char *hostname,
                                  const char *fallback_hostname,
                                  gboolean *out_changed)
{
	char *contents = NULL;
	char **lines = NULL;
	GError *error = NULL;
	GString *new_contents = NULL;
	gsize contents_len = 0;
	gboolean success = FALSE;

	g_return_val_if_fail (hostname != NULL, FALSE);
	g_return_val_if_fail (out_changed != NULL, FALSE);

	if (!g_file_get_contents (SYSCONFDIR "/hosts", &contents, &contents_len, &error)) {
		nm_log_warn (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s",
		             error ? error->code : 0,
		             (error && error->message) ? error->message : "(unknown)");
		g_clear_error (&error);
		return FALSE;
	}

	/* Get the new /etc/hosts contents */
	lines = g_strsplit_set (contents, "\n\r", 0);
	new_contents = nm_policy_get_etc_hosts ((const char **) lines,
	                                        contents_len,
	                                        hostname,
	                                        fallback_hostname,
	                                        &error);
	g_strfreev (lines);
	g_free (contents);

	if (new_contents) {
		nm_log_info (LOGD_DNS, "Updating /etc/hosts with new system hostname");

		g_clear_error (&error);
		/* And actually update /etc/hosts */
		if (!g_file_set_contents (SYSCONFDIR "/hosts", new_contents->str, -1, &error)) {
			nm_log_warn (LOGD_DNS, "couldn't update " SYSCONFDIR "/hosts: (%d) %s",
			             error ? error->code : 0,
			             (error && error->message) ? error->message : "(unknown)");
			g_clear_error (&error);
		} else {
			success = TRUE;
			*out_changed = TRUE;
		}

		g_string_free (new_contents, TRUE);
	} else if (!error) {
		/* No change required */
		success = TRUE;
	} else {
		nm_log_warn (LOGD_DNS, "couldn't read " SYSCONFDIR "/hosts: (%d) %s",
		             error->code, error->message ? error->message : "(unknown)");
		g_clear_error (&error);
	}

	return success;
}