summaryrefslogtreecommitdiff
path: root/src/charon/plugins/sql/sql_attribute.c
blob: 1e5c289661c9f284d2bb432b92d398ca2db4e6e5 (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
/*
 * Copyright (C) 2008 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.
 *
 * $Id$
 */

#include "sql_attribute.h"

#include <daemon.h>

typedef struct private_sql_attribute_t private_sql_attribute_t;

/**
 * private data of sql_attribute
 */
struct private_sql_attribute_t {

	/**
	 * public functions
	 */
	sql_attribute_t public;
	
	/**
	 * database connection
	 */
	database_t *db;
	
	/**
	 * wheter to record lease history in lease table
	 */
	bool history;
};

/**
 * read a host_t address from the addresses table
 */
static host_t *host_from_chunk(chunk_t chunk)
{
	switch (chunk.len)
	{
		case 4:
			return host_create_from_chunk(AF_INET, chunk, 0);
		case 16:
			return host_create_from_chunk(AF_INET6, chunk, 0);
		default:
			return NULL;
	}
}

/**
 * lookup/insert an identity
 */
static u_int get_identity(private_sql_attribute_t *this, identification_t *id)
{
	enumerator_t *e;
	u_int row;
	
	/* look for peer identity in the identities table */
	e = this->db->query(this->db,
						"SELECT id FROM identities WHERE type = ? AND data = ?",
						DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
						DB_UINT);
						
	if (e && e->enumerate(e, &row))
	{
		e->destroy(e);
		return row;
	}
	DESTROY_IF(e);
	/* not found, insert new one */
	if (this->db->execute(this->db, &row,
				  "INSERT INTO identities (type, data) VALUES (?, ?)",
				  DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id)) == 1)
	{
		return row;
	}
	return 0;
}

/**
 * Lookup pool by name
 */
static u_int get_pool(private_sql_attribute_t *this, char *name, u_int *timeout)
{
	enumerator_t *e;
	u_int pool;

	e = this->db->query(this->db, "SELECT id, timeout FROM pools WHERE name = ?",
						DB_TEXT, name, DB_UINT, DB_UINT);
	if (e && e->enumerate(e, &pool, timeout))
	{
		e->destroy(e);
		return pool;
	}
	DBG1(DBG_CFG, "ip pool '%s' not found");
	return 0;
}

/**
 * Lookup a lease
 */
static host_t *get_address(private_sql_attribute_t *this, char *name,
						   u_int pool, u_int timeout, u_int identity)
{
	enumerator_t *e;
	u_int id;
	chunk_t address;
	host_t *host;
	time_t now = time(NULL);
	
	/* We check for leases for that identity first and for other expired
	 * leases afterwards. We select an address as a candidate, but double
	 * check if it is still valid in the update. This allows us to work
	 * without locking. */
	
	/* check for an existing lease for that identity  */
	while (TRUE)
	{
		e = this->db->query(this->db,
				"SELECT id, address FROM addresses "
				"WHERE pool = ? AND identity = ? AND released != 0 LIMIT 1",
				DB_UINT, pool, DB_UINT, identity, DB_UINT, DB_BLOB);
		if (!e || !e->enumerate(e, &id, &address))
		{
			DESTROY_IF(e);
			break;	
		}
		address = chunk_clonea(address);
		e->destroy(e);
		if (this->db->execute(this->db, NULL,
				"UPDATE addresses SET acquired = ?, released = 0 "
				"WHERE id = ? AND identity = ? AND released != 0",
				DB_UINT, now, DB_UINT, id, DB_UINT, identity) > 0)
		{
			host = host_from_chunk(address);
			if (host)
			{
				DBG1(DBG_CFG, "acquired existing lease "
					 "for address %H in pool '%s'", host, name);
				return host;
			}
		}
	}
	
	/* check for an expired lease */
	while (TRUE)
	{
		e = this->db->query(this->db,
				"SELECT id, address FROM addresses "
				"WHERE pool = ? AND released != 0 AND released < ? LIMIT 1",
				DB_UINT, pool, DB_UINT, now - timeout, DB_UINT, DB_BLOB);
		if (!e || !e->enumerate(e, &id, &address))
		{
			DESTROY_IF(e);
			break;	
		}
		address = chunk_clonea(address);
		e->destroy(e);
			
		if (this->db->execute(this->db, NULL,
				"UPDATE addresses SET "
				"acquired = ?, released = 0, identity = ? "
				"WHERE id = ? AND released != 0 AND released < ?",
				DB_UINT, now, DB_UINT, identity,
				DB_UINT, id, DB_UINT, now - timeout) > 0)
		{
			host = host_from_chunk(address);
			if (host)
			{
				DBG1(DBG_CFG, "acquired new lease "
					 "for address %H in pool '%s'", host, name);
				return host;
			}
		}
	}
	DBG1(DBG_CFG, "no available address found in pool '%s'", name);
	return 0;
}

/**
 * Implementation of attribute_provider_t.acquire_address
 */
static host_t* acquire_address(private_sql_attribute_t *this,
							   char *name, identification_t *id,
							   auth_info_t *auth, host_t *requested)
{
	enumerator_t *enumerator;
	u_int pool, timeout, identity;
	host_t *address = NULL;
	
	identity = get_identity(this, id);
	if (identity)
	{
		enumerator = enumerator_create_token(name, ",", " ");
		while (enumerator->enumerate(enumerator, &name))
		{
			pool = get_pool(this, name, &timeout);
			if (pool)
			{
				address = get_address(this, name, pool, timeout, identity);
				if (address)
				{
					break;
				}
			}
		}
		enumerator->destroy(enumerator);
	}
	return address;
}

/**
 * Implementation of attribute_provider_t.release_address
 */
static bool release_address(private_sql_attribute_t *this,
							char *name, host_t *address)
{
	enumerator_t *enumerator;
	bool found = FALSE;
	time_t now = time(NULL);
	
	enumerator = enumerator_create_token(name, ",", " ");
	while (enumerator->enumerate(enumerator, &name))
	{
		u_int pool, timeout;
		
		pool = get_pool(this, name, &timeout);
		if (pool)
		{
			if (this->history)
			{
				this->db->execute(this->db, NULL,
					"INSERT INTO leases (address, identity, acquired, released)"
					" SELECT id, identity, acquired, ? FROM addresses "
					" WHERE pool = ? AND address = ?",
					DB_UINT, now, DB_UINT, pool,
					DB_BLOB, address->get_address(address));
			}
			if (this->db->execute(this->db, NULL,
					"UPDATE addresses SET released = ? WHERE "
					"pool = ? AND address = ?", DB_UINT, time(NULL),
					DB_UINT, pool, DB_BLOB, address->get_address(address)) > 0)
			{
				found = TRUE;
				break;
			}
		}
	}
	enumerator->destroy(enumerator);
	return found;
}

/**
 * Implementation of sql_attribute_t.destroy
 */
static void destroy(private_sql_attribute_t *this)
{
	free(this);
}

/*
 * see header file
 */
sql_attribute_t *sql_attribute_create(database_t *db)
{
	private_sql_attribute_t *this = malloc_thing(private_sql_attribute_t);
	time_t now = time(NULL);
	
	this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *,auth_info_t *, host_t *))acquire_address;
	this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *))release_address;
	this->public.destroy = (void(*)(sql_attribute_t*))destroy;
	
	this->db = db;
	this->history = lib->settings->get_bool(lib->settings,
									"charon.plugins.sql.lease_history", TRUE);
	
	/* close any "online" leases in the case we crashed */
	if (this->history)
	{
		this->db->execute(this->db, NULL,
					"INSERT INTO leases (address, identity, acquired, released)"
					" SELECT id, identity, acquired, ? FROM addresses "
					" WHERE released = 0", DB_UINT, now);
	}
	this->db->execute(this->db, NULL,
					  "UPDATE addresses SET released = ? WHERE released = 0",
					  DB_UINT, now);
	return &this->public;
}