summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/attr/attr_provider.c
blob: 1de571c3f1bb07b4f2d846bfa5bbb50092d0b585 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (C) 2010 Tobias Brunner
 * Copyright (C) 2009 Martin Willi
 * 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 "attr_provider.h"

#include <time.h>

#include <daemon.h>
#include <utils/debug.h>
#include <collections/linked_list.h>
#include <threading/rwlock.h>

#define SERVER_MAX		2

typedef struct private_attr_provider_t private_attr_provider_t;
typedef struct attribute_entry_t attribute_entry_t;

/**
 * private data of attr_provider
 */
struct private_attr_provider_t {

	/**
	 * public functions
	 */
	attr_provider_t public;

	/**
	 * List of attributes, attribute_entry_t
	 */
	linked_list_t *attributes;

	/**
	 * Lock for attribute list
	 */
	rwlock_t *lock;
};

struct attribute_entry_t {
	/** type of attribute */
	configuration_attribute_type_t type;
	/** attribute value */
	chunk_t value;
	/** associated IKE version */
	ike_version_t ike;
};

/**
 * Destroy an entry
 */
static void attribute_destroy(attribute_entry_t *this)
{
	free(this->value.ptr);
	free(this);
}

/**
 * Data for attribute enumerator
 */
typedef struct {
	rwlock_t *lock;
	ike_version_t ike;
} enumerator_data_t;

/**
 * convert enumerator value from attribute_entry
 */
static bool attr_enum_filter(enumerator_data_t *data, attribute_entry_t **in,
			configuration_attribute_type_t *type, void* none, chunk_t *value)
{
	if ((*in)->ike == IKE_ANY || (*in)->ike == data->ike)
	{
		*type = (*in)->type;
		*value = (*in)->value;
		return TRUE;
	}
	return FALSE;
}

CALLBACK(attr_enum_destroy, void,
	enumerator_data_t *data)
{
	data->lock->unlock(data->lock);
	free(data);
}

METHOD(attribute_provider_t, create_attribute_enumerator, enumerator_t*,
	private_attr_provider_t *this, linked_list_t *pools,
	ike_sa_t *ike_sa, linked_list_t *vips)
{
	enumerator_data_t *data;

	if (vips->get_count(vips))
	{
		INIT(data,
			.lock = this->lock,
			.ike = ike_sa->get_version(ike_sa),
		);
		this->lock->read_lock(this->lock);
		return enumerator_create_filter(
				this->attributes->create_enumerator(this->attributes),
				(void*)attr_enum_filter, data, attr_enum_destroy);
	}
	return enumerator_create_empty();
}

METHOD(attr_provider_t, destroy, void,
	private_attr_provider_t *this)
{
	this->attributes->destroy_function(this->attributes,
									   (void*)attribute_destroy);
	this->lock->destroy(this->lock);
	free(this);
}

/**
 * Add an attribute entry to the list
 */
static void add_legacy_entry(private_attr_provider_t *this, char *key, int nr,
							 configuration_attribute_type_t type)
{
	attribute_entry_t *entry;
	host_t *host;
	char *str;

	str = lib->settings->get_str(lib->settings, "%s.%s%d", NULL, lib->ns,
								 key, nr);
	if (str)
	{
		host = host_create_from_string(str, 0);
		if (host)
		{
			if (host->get_family(host) == AF_INET6)
			{
				switch (type)
				{
					case INTERNAL_IP4_DNS:
						type = INTERNAL_IP6_DNS;
						break;
					case INTERNAL_IP4_NBNS:
						type = INTERNAL_IP6_NBNS;
						break;
					default:
						break;
				}
			}
			INIT(entry,
				.type = type,
				.value = chunk_clone(host->get_address(host)),
				.ike = IKE_ANY,
			);
			host->destroy(host);
			DBG2(DBG_CFG, "loaded legacy entry attribute %N: %#B",
				 configuration_attribute_type_names, entry->type, &entry->value);
			this->attributes->insert_last(this->attributes, entry);
		}
	}
}

/**
 * Key to attribute type mappings, for v4 and v6 attributes
 */
typedef struct {
	char *name;
	configuration_attribute_type_t v4;
	configuration_attribute_type_t v6;
	ike_version_t ike;
} attribute_type_key_t;

static attribute_type_key_t keys[] = {
	{"address",			INTERNAL_IP4_ADDRESS,	INTERNAL_IP6_ADDRESS,	IKE_ANY},
	{"dns",				INTERNAL_IP4_DNS,		INTERNAL_IP6_DNS,		IKE_ANY},
	{"nbns",			INTERNAL_IP4_NBNS,		INTERNAL_IP6_NBNS,		IKE_ANY},
	{"dhcp",			INTERNAL_IP4_DHCP,		INTERNAL_IP6_DHCP,		IKE_ANY},
	{"netmask",			INTERNAL_IP4_NETMASK,	INTERNAL_IP6_NETMASK,	IKE_ANY},
	{"server",			INTERNAL_IP4_SERVER,	INTERNAL_IP6_SERVER,	IKE_ANY},
	{"subnet",			INTERNAL_IP4_SUBNET,	INTERNAL_IP6_SUBNET,	IKE_ANY},
	{"p-cscf",			P_CSCF_IP4_ADDRESS,		P_CSCF_IP6_ADDRESS,		IKEV2},
	{"split-include",	UNITY_SPLIT_INCLUDE,	UNITY_SPLIT_INCLUDE,	IKEV1},
	{"split-exclude",	UNITY_LOCAL_LAN,		UNITY_LOCAL_LAN,		IKEV1},
};

/**
 * Load (numerical) entries from the plugins.attr namespace
 */
static void load_entries(private_attr_provider_t *this)
{
	enumerator_t *enumerator, *tokens;
	char *key, *value, *token;
	int i;

	for (i = 1; i <= SERVER_MAX; i++)
	{
		add_legacy_entry(this, "dns", i, INTERNAL_IP4_DNS);
		add_legacy_entry(this, "nbns", i, INTERNAL_IP4_NBNS);
	}

	enumerator = lib->settings->create_key_value_enumerator(lib->settings,
													"%s.plugins.attr", lib->ns);
	while (enumerator->enumerate(enumerator, &key, &value))
	{
		configuration_attribute_type_t type;
		attribute_type_key_t *mapped = NULL;
		attribute_entry_t *entry;
		chunk_t data;
		host_t *host;
		char *pos;
		int i, mask = -1, family;

		if (streq(key, "load"))
		{
			continue;
		}
		type = atoi(key);
		if (!type)
		{
			for (i = 0; i < countof(keys); i++)
			{
				if (streq(key, keys[i].name))
				{
					mapped = &keys[i];
					break;
				}
			}
			if (!mapped)
			{
				DBG1(DBG_CFG, "mapping attribute type %s failed", key);
				continue;
			}
		}
		tokens = enumerator_create_token(value, ",", " ");
		while (tokens->enumerate(tokens, &token))
		{
			pos = strchr(token, '/');
			if (pos)
			{
				*(pos++) = '\0';
				mask = atoi(pos);
			}
			host = host_create_from_string(token, 0);
			if (!host)
			{
				if (mapped)
				{
					DBG1(DBG_CFG, "invalid host in key %s: %s", key, token);
					continue;
				}
				/* store numeric attributes that are no IP addresses as strings */
				data = chunk_clone(chunk_from_str(token));
			}
			else
			{
				family = host->get_family(host);
				if (mask == -1)
				{
					data = chunk_clone(host->get_address(host));
				}
				else
				{
					if (family == AF_INET)
					{	/* IPv4 attributes contain a subnet mask */
						u_int32_t netmask = 0;

						if (mask)
						{	/* shifting u_int32_t by 32 or more is undefined */
							mask = 32 - mask;
							netmask = htonl((0xFFFFFFFF >> mask) << mask);
						}
						data = chunk_cat("cc", host->get_address(host),
										 chunk_from_thing(netmask));
					}
					else
					{	/* IPv6 addresses the prefix only */
						data = chunk_cat("cc", host->get_address(host),
										 chunk_from_chars(mask));
					}
				}
				host->destroy(host);
				if (mapped)
				{
					switch (family)
					{
						case AF_INET:
							type = mapped->v4;
							break;
						case AF_INET6:
							type = mapped->v6;
							break;
					}
				}
			}
			INIT(entry,
				.type = type,
				.value = data,
				.ike = mapped ? mapped->ike : IKE_ANY,
			);
			DBG2(DBG_CFG, "loaded attribute %N: %#B",
				 configuration_attribute_type_names, entry->type, &entry->value);
			this->attributes->insert_last(this->attributes, entry);
		}
		tokens->destroy(tokens);
	}
	enumerator->destroy(enumerator);
}

METHOD(attr_provider_t, reload, void,
	private_attr_provider_t *this)
{
	this->lock->write_lock(this->lock);

	this->attributes->destroy_function(this->attributes, (void*)attribute_destroy);
	this->attributes = linked_list_create();

	load_entries(this);

	DBG1(DBG_CFG, "loaded %d entr%s for attr plugin configuration",
		 this->attributes->get_count(this->attributes),
		 this->attributes->get_count(this->attributes) == 1 ? "y" : "ies");

	this->lock->unlock(this->lock);
}

/*
 * see header file
 */
attr_provider_t *attr_provider_create(database_t *db)
{
	private_attr_provider_t *this;

	INIT(this,
		.public = {
			.provider = {
				.acquire_address = (void*)return_null,
				.release_address = (void*)return_false,
				.create_attribute_enumerator = _create_attribute_enumerator,
			},
			.reload = _reload,
			.destroy = _destroy,
		},
		.attributes = linked_list_create(),
		.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
	);

	load_entries(this);

	return &this->public;
}