summaryrefslogtreecommitdiff
path: root/service/realm-settings.c
blob: cb346ff03feedbb0c707f25bcdf93ea2def6b388 (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
/* realmd -- Realm configuration service
 *
 * Copyright 2012 Red Hat Inc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@gnome.org>
 */

#include "config.h"

#include "realm-settings.h"

#include <glib.h>

static GHashTable *realm_conf = NULL;

void
realm_settings_add (const gchar *section,
                    const gchar *key,
                    const gchar *value)
{
	GHashTable *sect;

	sect = g_hash_table_lookup (realm_conf, section);
	if (sect == NULL) {
		sect = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
		g_hash_table_insert (realm_conf, g_strdup (section), sect);
	}

	g_hash_table_insert (sect, g_strdup (key), g_strdup (value));
}

gboolean
realm_settings_load (const gchar *file_path,
                     GError **error)
{
	GKeyFile *key_file = NULL;
	GHashTable *section;
	GError *err = NULL;
	gchar **groups;
	gchar **keys;
	gchar *value;
	gint i;
	gint j;

	key_file = g_key_file_new ();

	if (!g_key_file_load_from_file (key_file, file_path, G_KEY_FILE_NONE, error)) {
		g_key_file_free (key_file);
		return FALSE;
	}

	/* Build into a table of strings, simplifies memory handling */
	groups = g_key_file_get_groups (key_file, NULL);
	for (i = 0; groups[i] != NULL; i++) {
		section = g_hash_table_lookup (realm_conf, groups[i]);
		if (section == NULL) {
			section = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
			g_hash_table_insert (realm_conf, g_strdup (groups[i]), section);
		}

		keys = g_key_file_get_keys (key_file, groups[i], NULL, &err);
		g_return_val_if_fail (err == NULL, FALSE);

		for (j = 0; keys[j] != NULL; j++) {
			value = g_key_file_get_value (key_file, groups[i], keys[j], &err);
			g_return_val_if_fail (err == NULL, FALSE);
			g_hash_table_insert (section, g_strdup (keys[j]), value);
		}
		g_strfreev (keys);
	}
	g_strfreev (groups);

	g_key_file_free (key_file);
	return TRUE;
}

void
realm_settings_init (void)
{
	const gchar *default_conf = PRIVATE_DIR "/realmd-defaults.conf";
	const gchar *distro_conf = PRIVATE_DIR "/realmd-distro.conf";
	const gchar *admin_conf = SYSCONF_DIR "/realmd.conf";
	GError *error = NULL;

	realm_conf = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
	                                    (GDestroyNotify)g_hash_table_unref);

	/*
	 * These are treated like 'linker error' in that we cannot proceed without
	 * this data. The reason it is not compiled into the daemon itself, is
	 * for easier modification by packagers and distros
	 */
	realm_settings_load (default_conf, &error);
	if (error != NULL) {
		g_error ("couldn't load package configuration file: %s: %s",
		         default_conf, error->message);
		g_clear_error (&error);
	}

	realm_settings_load (distro_conf, &error);
	if (error != NULL) {
		g_error ("couldn't load distro configuration file: %s: %s",
		         distro_conf, error->message);
		g_clear_error (&error);
	}

	/* We allow failure of loading or parsing this data, it's only overrides */
	realm_settings_load (admin_conf, &error);
	if (error != NULL) {
		if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
			g_message ("couldn't load admin configuration file: %s: %s",
			           admin_conf, error->message);
		}
		admin_conf = NULL;
		g_clear_error (&error);
	}

	g_debug ("Loaded settings from: %s %s %s",
	         default_conf, distro_conf,
	         admin_conf ? admin_conf : "");
}

void
realm_settings_uninit (void)
{
	g_assert (realm_conf != NULL);
	g_hash_table_destroy (realm_conf);
	realm_conf = NULL;
}

const gchar *
realm_settings_path (const gchar *name)
{
	const gchar *path;

	path = realm_settings_value ("paths", name);
	if (path == NULL) {
		g_warning ("no path found for '%s' in realmd config", name);
		return "/invalid/or/misconfigured";
	}

	return path;
}

GHashTable *
realm_settings_section (const gchar *section)
{
	return g_hash_table_lookup (realm_conf, section);
}

const gchar *
realm_settings_value (const gchar *section,
                      const gchar *key)
{
	GHashTable *settings;

	settings = realm_settings_section (section);
	if (settings == NULL)
		return NULL;
	return g_hash_table_lookup (settings, key);
}

const gchar *
realm_settings_string (const gchar *section,
                       const gchar *key)
{
	const gchar *string;

	string = realm_settings_value (section, key);
	if (string == NULL) {
		g_warning ("no value found for '%s/%s' in realmd config", section, key);
		return "";
	}

	return string;
}

gdouble
realm_settings_double (const gchar *section,
                       const gchar *key,
                       gdouble def)
{
	const gchar *string;
	gchar *end_ptr = NULL;
	gdouble val;

	string = realm_settings_value (section, key);
	if (string == NULL)
		return def;

	val = g_ascii_strtod (string, &end_ptr);
	if (!end_ptr || *end_ptr != '\0') {
		g_critical ("invalid %s/%s floating point value '%s' in realmd config",
		            section, key, string);
		return def;
	}
	return val;
}

gboolean
realm_settings_boolean (const gchar *section,
                        const gchar *key,
                        gboolean def)
{
	const gchar *string;

	string = realm_settings_value (section, key);
	if (string == NULL)
		return def;

	return g_ascii_strcasecmp (string, "true") == 0 ||
	       g_ascii_strcasecmp (string, "1") == 0 ||
	       g_ascii_strcasecmp (string, "yes") == 0;
}