summaryrefslogtreecommitdiff
path: root/src/nm-config.c
blob: 1efad4511735368387bfde761e719f121e373adc (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* -*- 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) 2011 Red Hat, Inc.
 */

#include <config.h>
#include <string.h>
#include <stdio.h>

#include "nm-config.h"

#define NM_DEFAULT_SYSTEM_CONF_FILE  SYSCONFDIR"/NetworkManager/NetworkManager.conf"
#define NM_OLD_SYSTEM_CONF_FILE      SYSCONFDIR"/NetworkManager/nm-system-settings.conf"

struct NMConfig {
	char *path;
	char **plugins;
	char *dhcp_client;
	char **dns_plugins;
	char *log_level;
	char *log_domains;
	char *connectivity_uri;
	guint connectivity_interval;
	char *connectivity_response;
};

/************************************************************************/

GQuark
nm_config_error_quark (void)
{
	static GQuark quark = 0;
	if (!quark)
		quark = g_quark_from_static_string ("nm-config-error");
	return quark;
}

/************************************************************************/

const char *
nm_config_get_path (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return config->path;
}

const char **
nm_config_get_plugins (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return (const char **) config->plugins;
}

const char *
nm_config_get_dhcp_client (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return config->dhcp_client;
}

const char **
nm_config_get_dns_plugins (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return (const char **) config->dns_plugins;
}

const char *
nm_config_get_log_level (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return config->log_level;
}

const char *
nm_config_get_log_domains (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return config->log_domains;
}

const char *
nm_config_get_connectivity_uri (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return config->connectivity_uri;
}

const guint
nm_config_get_connectivity_interval (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, 0);

	return config->connectivity_interval;
}

const char *
nm_config_get_connectivity_response (NMConfig *config)
{
	g_return_val_if_fail (config != NULL, NULL);

	return config->connectivity_response;
}


/************************************************************************/

static gboolean
fill_from_file (NMConfig *config,
                const char *path,
                const char *cli_plugins,
                const char *cli_log_level,
                const char *cli_log_domains,
                const char *cli_connectivity_uri,
                const gint cli_connectivity_interval,
                const char *cli_connectivity_response,
                GError **error)
{
	GKeyFile *kf;
	gboolean success = FALSE;

	if (g_file_test (path, G_FILE_TEST_EXISTS) == FALSE) {
		g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND, "file %s not found", path);
		return FALSE;
	}

	kf = g_key_file_new ();
	if (!kf) {
		g_set_error (error, NM_CONFIG_ERROR, NM_CONFIG_ERROR_NO_MEMORY,
		             "Not enough memory to load config file %s", path);
		return FALSE;
	}

	g_key_file_set_list_separator (kf, ',');
	if (g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, error)) {
		config->path = g_strdup (path);

		/* CLI provided options override config file options */
		if (cli_plugins && strlen (cli_plugins))
			config->plugins = g_strsplit_set (cli_plugins, ",", 0);
		else
			config->plugins = g_key_file_get_string_list (kf, "main", "plugins", NULL, NULL);

		config->dhcp_client = g_key_file_get_value (kf, "main", "dhcp", NULL);
		config->dns_plugins = g_key_file_get_string_list (kf, "main", "dns", NULL, NULL);

		if (cli_log_level && strlen (cli_log_level))
			config->log_level = g_strdup (cli_log_level);
		else
			config->log_level = g_key_file_get_value (kf, "logging", "level", NULL);

		if (cli_log_domains && strlen (cli_log_domains))
			config->log_domains = g_strdup (cli_log_domains);
		else
			config->log_domains = g_key_file_get_value (kf, "logging", "domains", NULL);

		if (cli_connectivity_uri && strlen (cli_connectivity_uri))
			config->connectivity_uri = g_strdup (cli_connectivity_uri);
		else
			config->connectivity_uri = g_key_file_get_value (kf, "connectivity", "uri", NULL);

		if (cli_connectivity_interval >= 0)
			config->connectivity_interval = cli_connectivity_interval;
		else
			config->connectivity_interval = g_key_file_get_integer (kf, "connectivity", "interval", NULL);

		if (cli_connectivity_response && strlen (cli_connectivity_response))
			config->connectivity_response = g_strdup (cli_connectivity_response);
		else
			config->connectivity_response = g_key_file_get_value (kf, "connectivity", "response", NULL);

		success = TRUE;
	}

	g_key_file_free (kf);
	return success;
}

NMConfig *
nm_config_new (const char *cli_config_path,
               const char *cli_plugins,
               const char *cli_log_level,
               const char *cli_log_domains,
               const char *cli_connectivity_uri,
               const gint cli_connectivity_interval,
               const char *cli_connectivity_response,
               GError **error)
{
	NMConfig *config;
	GError *local = NULL;

	config = g_malloc0 (sizeof (*config));

	if (cli_config_path) {
		/* Bad user-specific config file path is a hard error */
		if (!fill_from_file (config, cli_config_path, cli_plugins, cli_log_level, cli_log_domains,
		                     cli_connectivity_uri, cli_connectivity_interval, cli_connectivity_response,
		                     error)) {
			nm_config_free (config);
			return NULL;
		}
		return config;
	}

	/* Even though we prefer NetworkManager.conf, we need to check the
	 * old nm-system-settings.conf first to preserve compat with older
	 * setups.  In package managed systems dropping a NetworkManager.conf
	 * onto the system would make NM use it instead of nm-system-settings.conf,
	 * changing behavior during an upgrade.  We don't want that.
	 */

	/* Try deprecated nm-system-settings.conf first */
	if (fill_from_file (config, NM_OLD_SYSTEM_CONF_FILE, cli_plugins, cli_log_level, cli_log_domains,
	                    cli_connectivity_uri, cli_connectivity_interval, cli_connectivity_response,
	                    &local))
		return config;

	if (g_error_matches (local, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND) == FALSE) {
		fprintf (stderr, "Default config file %s invalid: (%d) %s\n",
		         NM_OLD_SYSTEM_CONF_FILE,
		         local ? local->code : -1,
		         (local && local->message) ? local->message : "unknown");
	}
	g_clear_error (&local);

	/* Try the standard config file location next */
	if (fill_from_file (config, NM_DEFAULT_SYSTEM_CONF_FILE, cli_plugins, cli_log_level, cli_log_domains,
	                    cli_connectivity_uri, cli_connectivity_interval, cli_connectivity_response,
	                    &local))
		return config;

	if (g_error_matches (local, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND) == FALSE) {
		fprintf (stderr, "Default config file %s invalid: (%d) %s\n",
		         NM_DEFAULT_SYSTEM_CONF_FILE,
		         local ? local->code : -1,
		         (local && local->message) ? local->message : "unknown");
		g_propagate_error (error, local);
		nm_config_free (config);
		return NULL;
	}

	/* If for some reason no config file exists, and NM wasn't given on on
	 * the command line, just use the default config file path.
	 */
	if (config->path == NULL) {
		config->path = g_strdup (NM_DEFAULT_SYSTEM_CONF_FILE);
		fprintf (stderr, "No config file found or given; using %s\n",
		         NM_DEFAULT_SYSTEM_CONF_FILE);
	}

	/* ignore error if config file not found */
	g_clear_error (&local);
	return config;
}

void
nm_config_free (NMConfig *config)
{
	g_return_if_fail (config != NULL);

	g_free (config->path);
	g_strfreev (config->plugins);
	g_free (config->dhcp_client);
	g_strfreev (config->dns_plugins);
	g_free (config->log_level);
	g_free (config->log_domains);
	g_free (config->connectivity_uri);
	g_free (config->connectivity_response);
	memset (config, 0, sizeof (*config));
	g_free (config);
}