summaryrefslogtreecommitdiff
path: root/service/realm-sssd-config.c
blob: d02b01ff813f2db4fcbc861bd5b1105f126443ff (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
/* 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-errors.h"
#include "realm-ini-config.h"
#include "realm-sssd-config.h"
#include "realm-settings.h"

#include <glib/gi18n.h>

#include <string.h>

RealmIniConfig *
realm_sssd_config_new_with_flags (RealmIniFlags flags,
                                  GError **error)
{
	RealmIniConfig *config;
	const gchar *filename;
	GError *err = NULL;

	config = realm_ini_config_new (flags | REALM_INI_PRIVATE | REALM_INI_STRICT_BOOLEAN);

	filename = realm_settings_path ("sssd.conf");
	realm_ini_config_read_file (config, filename, &err);

	if (err != NULL) {
		/* If the caller wants errors, then don't return an invalid samba config */
		if (error) {
			g_propagate_error (error, err);
			g_object_unref (config);
			config = NULL;

		/* If the caller doesn't care, then warn but continue */
		} else {
			g_warning ("Couldn't load config file: %s: %s", filename,
			           err->message);
			g_error_free (err);
		}
	}

	return config;
}

RealmIniConfig *
realm_sssd_config_new (GError **error)
{
	return realm_sssd_config_new_with_flags (REALM_INI_NONE, error);
}

gchar **
realm_sssd_config_get_domains (RealmIniConfig *config)
{
	g_return_val_if_fail (REALM_IS_INI_CONFIG (config), NULL);
	return realm_ini_config_get_list (config, "sssd", "domains", ",");
}

gchar *
realm_sssd_config_domain_to_section (const gchar *domain)
{
	g_return_val_if_fail (domain != NULL, NULL);
	return g_strdup_printf ("domain/%s", domain);
}

gboolean
realm_sssd_config_have_domain (RealmIniConfig *config,
                               const gchar *domain)
{
	gchar **domains;
	gboolean have = FALSE;
	gint i;

	g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE);
	g_return_val_if_fail (domain != NULL, FALSE);

	domains = realm_sssd_config_get_domains (config);
	for (i = 0; domains && domains[i] != NULL; i++) {
		if (g_str_equal (domain, domains[i])) {
			have = TRUE;
			break;
		}
	}
	g_strfreev (domains);

	return have;
}

static gboolean
update_domain (RealmIniConfig *config,
               const char *section,
               va_list va,
               GError **error)
{
	GHashTable *parameters;
	const gchar *name;
	const gchar *value;

	parameters = g_hash_table_new (g_str_hash, g_str_equal);
	while ((name = va_arg (va, const gchar *)) != NULL) {
		value = va_arg (va, const gchar *);
		g_hash_table_insert (parameters, (gpointer)name, (gpointer)value);
	}

	realm_ini_config_set_all (config, section, parameters);
	g_hash_table_unref (parameters);

	return realm_ini_config_finish_change (config, error);

}

gboolean
realm_sssd_config_add_domain (RealmIniConfig *config,
                              const gchar *domain,
                              GError **error,
                              ...)
{
	const gchar *domains[2];
	gchar **already;
	gboolean ret;
	gchar *section;
	const gchar *services[] = { "nss", "pam", NULL };
	va_list va;
	gint i;

	g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE);
	g_return_val_if_fail (domain != NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	if (!realm_ini_config_begin_change (config, error))
		return FALSE;

	already = realm_sssd_config_get_domains (config);
	for (i = 0; already && already[i] != NULL; i++) {
		if (g_str_equal (domain, already[i])) {
			realm_ini_config_abort_change (config);
			g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_EXIST,
			             _("Already have domain %s in sssd.conf config file"), domain);
			g_strfreev (already);
			return FALSE;
		}
	}

	g_strfreev (already);

	/* Setup a default sssd section */
	realm_ini_config_set_list_diff (config, "sssd", "services", ", ", services, NULL);

	domains[0] = domain;
	domains[1] = NULL;
	realm_ini_config_set_list_diff (config, "sssd", "domains", ", ", domains, NULL);

	section = realm_sssd_config_domain_to_section (domain);

	va_start (va, error);
	ret = update_domain (config, section, va, error);
	va_end (va);

	g_free (section);

	return ret;
}

gboolean
realm_sssd_config_update_domain (RealmIniConfig *config,
                                 const gchar *domain,
                                 GError **error,
                                 ...)
{
	gchar *section;
	gboolean ret;
	va_list va;

	g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE);
	g_return_val_if_fail (domain != NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	if (!realm_ini_config_begin_change (config, error))
		return FALSE;

	section = realm_sssd_config_domain_to_section (domain);
	if (!realm_ini_config_have_section (config, section)) {
		realm_ini_config_abort_change (config);
		g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT,
		             _("Don't have domain %s in sssd.conf config file"), domain);
		g_free (section);
		return FALSE;
	}

	va_start (va, error);
	ret = update_domain (config, section, va, error);
	va_end (va);

	g_free (section);

	return ret;
}

gboolean
realm_sssd_config_remove_domain (RealmIniConfig *config,
                                 const gchar *domain,
                                 GError **error)
{
	const gchar *domains[2];
	gchar *section;

	g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE);
	g_return_val_if_fail (domain != NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	if (!realm_ini_config_begin_change (config, error))
		return FALSE;

	section = realm_sssd_config_domain_to_section (domain);

	domains[0] = domain;
	domains[1] = NULL;
	realm_ini_config_set_list_diff (config, "sssd", "domains", ", ", NULL, domains);
	realm_ini_config_remove_section (config, section);
	g_free (section);

	return realm_ini_config_finish_change (config, error);
}

gboolean
realm_sssd_config_load_domain (RealmIniConfig *config,
                               const gchar *domain,
                               gchar **out_section,
                               gchar **id_provider,
                               gchar **realm_name)
{
	const gchar *field_name;
	gchar *section;
	gchar *type;
	gchar *name;

	g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE);
	g_return_val_if_fail (domain != NULL, FALSE);

	section = realm_sssd_config_domain_to_section (domain);
	type = realm_ini_config_get (config, section, "id_provider");

	if (g_strcmp0 (type, "ad") == 0) {
		field_name = "ad_domain";

	} else if (g_strcmp0 (type, "ipa") == 0) {
		field_name = "ipa_domain";

	} else {
		g_free (section);
		g_free (type);
		return FALSE;
	}

	name = realm_ini_config_get (config, section, field_name);
	if (name == NULL)
		name = realm_ini_config_get (config, section, "krb5_realm");
	if (name == NULL)
		name = g_strdup (domain);

	if (realm_name) {
		*realm_name = name;
		name = NULL;
	}

	if (id_provider) {
		*id_provider = type;
		type = NULL;
	}

	if (out_section) {
		*out_section = section;
		section = NULL;
	}

	g_free (type);
	g_free (section);
	g_free (name);
	return TRUE;
}