summaryrefslogtreecommitdiff
path: root/src/libstrongswan/settings/settings_types.c
blob: d753720f5ef4b9040a23a80dc0eb70fc7da0c3d2 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2010-2014 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include "settings_types.h"

/*
 * Described in header
 */
kv_t *settings_kv_create(char *key, char *value)
{
	kv_t *this;

	INIT(this,
		.key = key,
		.value = value,
	);
	return this;
}

/*
 * Described in header
 */
void settings_kv_destroy(kv_t *this, array_t *contents)
{
	free(this->key);
	if (contents && this->value)
	{
		array_insert(contents, ARRAY_TAIL, this->value);
	}
	else
	{
		free(this->value);
	}
	free(this);
}

/*
 * Described in header
 */
section_t *settings_section_create(char *name)
{
	section_t *this;

	INIT(this,
		.name = name,
	);
	return this;
}

static void section_destroy(section_t *section, int idx, array_t *contents)
{
	settings_section_destroy(section, contents);
}

static void kv_destroy(kv_t *kv, int idx, array_t *contents)
{
	settings_kv_destroy(kv, contents);
}

/*
 * Described in header
 */
void settings_section_destroy(section_t *this, array_t *contents)
{
	array_destroy_function(this->sections, (void*)section_destroy, contents);
	array_destroy(this->sections_order);
	array_destroy_function(this->kv, (void*)kv_destroy, contents);
	array_destroy(this->kv_order);
	array_destroy(this->fallbacks);
	free(this->name);
	free(this);
}

/*
 * Described in header
 */
void settings_kv_set(kv_t *kv, char *value, array_t *contents)
{
	if (value && kv->value && streq(value, kv->value))
	{	/* no update required */
		free(value);
		return;
	}

	/* if the new value was shorter we could overwrite the existing one but that
	 * could lead to reads of partially updated values from other threads that
	 * have a pointer to the existing value, so we replace it anyway */
	if (kv->value && contents)
	{
		array_insert(contents, ARRAY_TAIL, kv->value);
	}
	else
	{
		free(kv->value);
	}
	kv->value = value;
}

/*
 * Described in header
 */
void settings_kv_add(section_t *section, kv_t *kv, array_t *contents)
{
	kv_t *found;

	if (array_bsearch(section->kv, kv->key, settings_kv_find, &found) == -1)
	{
		array_insert_create(&section->kv, ARRAY_TAIL, kv);
		array_sort(section->kv, settings_kv_sort, NULL);
		array_insert_create(&section->kv_order, ARRAY_TAIL, kv);
	}
	else
	{
		settings_kv_set(found, kv->value, contents);
		kv->value = NULL;
		settings_kv_destroy(kv, NULL);
	}
}

/*
 * Add a section to the given parent, optionally remove settings/subsections
 * not found when extending an existing section
 */
static void add_section(section_t *parent, section_t *section,
						array_t *contents, bool purge)
{
	section_t *found;

	if (array_bsearch(parent->sections, section->name, settings_section_find,
					  &found) == -1)
	{
		array_insert_create(&parent->sections, ARRAY_TAIL, section);
		array_sort(parent->sections, settings_section_sort, NULL);
		array_insert_create(&parent->sections_order, ARRAY_TAIL, section);
	}
	else
	{
		settings_section_extend(found, section, contents, purge);
		settings_section_destroy(section, contents);
	}
}

/*
 * Described in header
 */
void settings_section_add(section_t *parent, section_t *section,
						  array_t *contents)
{
	add_section(parent, section, contents, FALSE);
}

/**
 * Purge contents of a section, returns TRUE if section can be safely removed.
 */
static bool section_purge(section_t *this, array_t *contents)
{
	section_t *current;
	int i, idx;

	array_destroy_function(this->kv, (void*)kv_destroy, contents);
	this->kv = NULL;
	array_destroy(this->kv_order);
	this->kv_order = NULL;
	/* we ensure sections used as fallback, or configured with fallbacks (or
	 * having any such subsections) are not removed */
	for (i = array_count(this->sections_order) - 1; i >= 0; i--)
	{
		array_get(this->sections_order, i, &current);
		if (section_purge(current, contents))
		{
			array_remove(this->sections_order, i, NULL);
			idx = array_bsearch(this->sections, current->name,
								settings_section_find, NULL);
			array_remove(this->sections, idx, NULL);
			settings_section_destroy(current, contents);
		}
	}
	return !this->fallbacks && !array_count(this->sections);
}

/*
 * Described in header
 */
void settings_section_extend(section_t *base, section_t *extension,
							 array_t *contents, bool purge)
{
	enumerator_t *enumerator;
	section_t *section;
	kv_t *kv;
	array_t *sections = NULL, *kvs = NULL;
	int idx;

	if (purge)
	{	/* remove sections and settings in base not found in extension, the
		 * others are removed too (from the _order list) so they can be inserted
		 * in the order found in extension */
		enumerator = array_create_enumerator(base->sections_order);
		while (enumerator->enumerate(enumerator, (void**)&section))
		{
			if (array_bsearch(extension->sections, section->name,
							  settings_section_find, NULL) == -1)
			{
				idx = array_bsearch(base->sections, section->name,
									settings_section_find, NULL);
				if (section_purge(section, contents))
				{	/* only remove them if we can purge them */
					array_remove(base->sections, idx, NULL);
					array_remove_at(base->sections_order, enumerator);
					settings_section_destroy(section, contents);
				}
			}
			else
			{
				array_remove_at(base->sections_order, enumerator);
				array_insert_create(&sections, ARRAY_TAIL, section);
				array_sort(sections, settings_section_sort, NULL);
			}
		}
		enumerator->destroy(enumerator);

		while (array_remove(base->kv_order, 0, &kv))
		{
			if (array_bsearch(extension->kv, kv->key, settings_kv_find,
							  NULL) == -1)
			{
				idx = array_bsearch(base->kv, kv->key, settings_kv_find, NULL);
				array_remove(base->kv, idx, NULL);
				settings_kv_destroy(kv, contents);
			}
			else
			{
				array_insert_create(&kvs, ARRAY_TAIL, kv);
				array_sort(kvs, settings_kv_sort, NULL);
			}
		}
	}

	while (array_remove(extension->sections_order, 0, &section))
	{
		idx = array_bsearch(sections, section->name,
							settings_section_find, NULL);
		if (idx != -1)
		{
			section_t *existing;

			array_remove(sections, idx, &existing);
			array_insert(base->sections_order, ARRAY_TAIL, existing);
		}
		idx = array_bsearch(extension->sections, section->name,
							settings_section_find, NULL);
		array_remove(extension->sections, idx, NULL);
		add_section(base, section, contents, purge);
	}

	while (array_remove(extension->kv_order, 0, &kv))
	{
		idx = array_bsearch(kvs, kv->key, settings_kv_find, NULL);
		if (idx != -1)
		{
			kv_t *existing;

			array_remove(kvs, idx, &existing);
			array_insert(base->kv_order, ARRAY_TAIL, existing);
		}
		idx = array_bsearch(extension->kv, kv->key, settings_kv_find, NULL);
		array_remove(extension->kv, idx, NULL);
		settings_kv_add(base, kv, contents);
	}
	array_destroy(sections);
	array_destroy(kvs);
}

/*
 * Described in header
 */
int settings_section_find(const void *a, const void *b)
{
	const char *key = a;
	const section_t *item = b;
	return strcmp(key, item->name);
}

/*
 * Described in header
 */
int settings_section_sort(const void *a, const void *b, void *user)
{
	const section_t *sa = a, *sb = b;
	return strcmp(sa->name, sb->name);
}

/*
 * Described in header
 */
int settings_kv_find(const void *a, const void *b)
{
	const char *key = a;
	const kv_t *item = b;
	return strcmp(key, item->key);
}

/*
 * Described in header
 */
int settings_kv_sort(const void *a, const void *b, void *user)
{
	const kv_t *kva = a, *kvb = b;
	return strcmp(kva->key, kvb->key);
}