summaryrefslogtreecommitdiff
path: root/lib/gnutls_db.c
blob: df7f47248d044ac57fe802739a0a6fe1c554d9d8 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 *      Copyright (C) 2000 Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * GNUTLS 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.
 *
 * GNUTLS 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

/* This file contains functions that manipulate a database
 * for resumed sessions. 
 */
#include "gnutls_int.h"
#include "gnutls_errors.h"
#include "gnutls_session.h"
#include <gnutls_db.h>
#include "debug.h"

#define GNUTLS_DBNAME state->gnutls_internals.db_name

#ifdef HAVE_LIBGDBM
# define GNUTLS_DBF state->gnutls_internals.db_reader
# define GNUTLS_REOPEN_DB() if (GNUTLS_DBF!=NULL) \
	gdbm_close( GNUTLS_DBF); \
	GNUTLS_DBF = gdbm_open(GNUTLS_DBNAME, 0, GDBM_READER, 0600, NULL);
#endif

/**
  * gnutls_set_db_retrieve_function - Sets the function that will be used to get data
  * @state: is a &GNUTLS_STATE structure.
  * @retr_func: is the function.
  *
  * Sets the function that will be used to retrieve data from the resumed
  * sessions database. This function must return a gnutls_datum containing the
  * data on success, or a gnutls_datum containing null and 0 on failure.
  * This function should only be used if you do
  * not plan to use the included gdbm backend.
  *
  * The first argument to store_func() will be null unless gnutls_db_set_db_ptr() 
  * has been called.
  *
  **/
void gnutls_set_db_retrieve_function( GNUTLS_STATE state, DB_RETR_FUNC retr_func) {
	state->gnutls_internals.db_retrieve_func = retr_func;
}

/**
  * gnutls_set_db_remove_function - Sets the function that will be used to remove data
  * @state: is a &GNUTLS_STATE structure.
  * @rem_func: is the function.
  *
  * Sets the function that will be used to remove data from the resumed
  * sessions database. This function must return 0 on success.
  * This function should only be used if you do
  * not plan to use the included gdbm backend.
  *
  * The first argument to rem_func() will be null unless gnutls_db_set_db_ptr() 
  * has been called.
  *
  **/
void gnutls_set_db_remove_function( GNUTLS_STATE state, DB_REMOVE_FUNC rem_func) {
	state->gnutls_internals.db_remove_func = rem_func;
}

/**
  * gnutls_set_db_store_function - Sets the function that will be used to put data
  * @state: is a &GNUTLS_STATE structure.
  * @store_func: is the function
  *
  * Sets the function that will be used to store data from the resumed
  * sessions database. This function must remove 0 on success. 
  * This function should only be used if you do
  * not plan to use the included gdbm backend.
  *
  * The first argument to store_func() will be null unless gnutls_db_set_ptr() 
  * has been called.
  *
  **/
void gnutls_set_db_store_function( GNUTLS_STATE state, DB_STORE_FUNC store_func) {
	state->gnutls_internals.db_store_func = store_func;
}

/**
  * gnutls_set_db_ptr - Sets a pointer to be sent to db functions
  * @state: is a &GNUTLS_STATE structure.
  * @ptr: is the pointer
  *
  * Sets the pointer that will be sent to db store, retrieve and delete functions, as
  * the first argument. Should only be called if not using the gdbm backend.
  *
  **/
void gnutls_set_db_ptr( GNUTLS_STATE state, void* ptr) {
	state->gnutls_internals.db_ptr = ptr;
}

/**
  * gnutls_set_cache_expiration - Sets the expiration time for resumed sessions.
  * @state: is a &GNUTLS_STATE structure.
  * @seconds: is the number of seconds.
  *
  * Sets the expiration time for resumed sessions. The default is 3600 (one hour)
  * at the time writing this.
  **/
int gnutls_set_cache_expiration( GNUTLS_STATE state, int seconds) {
	state->gnutls_internals.expire_time = seconds;
	return 0;
}

/**
  * gnutls_set_db_name - Sets the name of the database that holds TLS sessions.
  * @state: is a &GNUTLS_STATE structure.
  * @filename: is the filename for the database
  *
  * Sets the name of the (gdbm) database to be used to keep
  * the sessions to be resumed. This function also creates the database
  * - if it does not exist - and opens it for reading.
  * You should not call this function if using an other backend
  * than gdbm (ie. called function gnutls_set_db_store_func() etc.)
  *
  **/
int gnutls_set_db_name( GNUTLS_STATE state, char* filename) {
#ifdef HAVE_LIBGDBM
GDBM_FILE dbf;

	if (filename==NULL) return 0;

	/* deallocate previous name */
	if (GNUTLS_DBNAME!=NULL)
		gnutls_free(GNUTLS_DBNAME);

	/* set name */
	GNUTLS_DBNAME = gnutls_strdup(filename);
	if (GNUTLS_DBNAME==NULL) return GNUTLS_E_MEMORY_ERROR;

	/* open for reader */
	GNUTLS_DBF = gdbm_open(GNUTLS_DBNAME, 0, GDBM_READER, 0600, NULL);
	if (GNUTLS_DBF==NULL) {
		/* maybe it does not exist - so try to
		 * create it.
		 */
		dbf = gdbm_open( filename, 0, GDBM_WRCREAT, 0600, NULL);
		if (dbf==NULL) return GNUTLS_E_DB_ERROR;
		gdbm_close(dbf);

		/* try to open again */
		GNUTLS_DBF = gdbm_open(GNUTLS_DBNAME, 0, GDBM_READER, 0600, NULL);
	}
	if (GNUTLS_DBF==NULL)
		return GNUTLS_E_DB_ERROR;

	return 0;
#else
	return GNUTLS_E_UNIMPLEMENTED_FEATURE;
#endif
}

/**
  * gnutls_check_db_entry - checks if the given db entry has expired
  * @state: is a &GNUTLS_STATE structure.
  * @session_entry: is the session data (not key)
  *
  * This function should only be used if not using the gdbm backend.
  * This function returns GNUTLS_E_EXPIRED, if the database entry
  * has expired or 0 otherwise. This function is to be used when
  * you want to clear unnesessary session which occupy space in your
  * backend.
  *
  **/
int gnutls_check_db_entry( GNUTLS_STATE state, gnutls_datum session_entry) {
time_t timestamp;

	timestamp = time(0);

	if (session_entry.data != NULL)
		if ( timestamp - ((SecurityParameters*)(session_entry.data))->timestamp <= state->gnutls_internals.expire_time || ((SecurityParameters*)(session_entry.data))->timestamp > timestamp|| ((SecurityParameters*)(session_entry.data))->timestamp == 0)
			return GNUTLS_E_EXPIRED;
	
	return 0;
}

/**
  * gnutls_clean_db - removes expired and invalid sessions from the database
  * @state: is a &GNUTLS_STATE structure.
  *
  * This function Deletes all expired records in the resumed sessions' database. 
  * This database may become huge if this function is not called.
  * This function is also quite expensive. This function should only
  * be called if using the gdbm backend.
  *
  **/
int gnutls_clean_db( GNUTLS_STATE state) {
#ifdef HAVE_LIBGDBM
GDBM_FILE dbf;
int ret;
datum key;
time_t timestamp;
gnutls_datum _key;

	if (GNUTLS_DBF==NULL) return GNUTLS_E_DB_ERROR;
	if (GNUTLS_DBNAME==NULL) return GNUTLS_E_DB_ERROR;

	dbf = gdbm_open(GNUTLS_DBNAME, 0, GDBM_WRITER, 0600, NULL);
	if (dbf==NULL) return GNUTLS_E_AGAIN;
	key = gdbm_firstkey(dbf);

	timestamp = time(0);

	_key.data = key.dptr;
	_key.size = key.dsize;
	while( _key.data != NULL) {

		if ( gnutls_check_db_entry( state, _key)==GNUTLS_E_EXPIRED) {
		    /* delete expired entry */
		    gdbm_delete( dbf, key);
		}
		
		free(key.dptr);
		key = gdbm_nextkey(dbf, key);
	}
	ret = gdbm_reorganize(dbf);
	
	gdbm_close(dbf);
	GNUTLS_REOPEN_DB();
	
	if (ret!=0) return GNUTLS_E_DB_ERROR;
		
	return 0;
#else
	return GNUTLS_E_UNIMPLEMENTED_FEATURE;
#endif

}

/* The format of storing data is:
 * SECURITY_PARAMETERS + AUTH_INFO_SIZE + AUTH_INFO
 */
int _gnutls_server_register_current_session( GNUTLS_STATE state)
{
gnutls_datum key = { state->security_parameters.session_id, state->security_parameters.session_id_size };
gnutls_datum content;
int ret = 0, pos;

	if (state->gnutls_internals.resumable==RESUME_FALSE) 
		return GNUTLS_E_INVALID_SESSION;

	if (state->security_parameters.session_id==NULL || state->security_parameters.session_id_size==0)
		return GNUTLS_E_INVALID_SESSION;

/* allocate space for data */
	content.size = sizeof(SecurityParameters) + state->gnutls_key->auth_info_size
		+ sizeof(state->gnutls_key->auth_info_size);
	content.data = gnutls_malloc( content.size);
	if (content.data==NULL) return GNUTLS_E_MEMORY_ERROR;

/* copy data */
	pos = 0;
	memcpy( &content.data[0], (void*)&state->security_parameters, sizeof(SecurityParameters));
	pos+=sizeof(SecurityParameters);

	memcpy( &content.data[pos], &state->gnutls_key->auth_info_size,  sizeof(state->gnutls_key->auth_info_size));
	pos+=sizeof(state->gnutls_key->auth_info_size);

	memcpy( &content.data[pos], state->gnutls_key->auth_info, state->gnutls_key->auth_info_size);

	ret = _gnutls_store_session( state, key, content);

	gnutls_free( content.data);

	return ret;
}

int _gnutls_server_restore_session( GNUTLS_STATE state, uint8* session_id, int session_id_size)
{
gnutls_datum data;
gnutls_datum key = { session_id, session_id_size };
int ret;

	data = _gnutls_retrieve_session( state, key);

	if (data.data==NULL) {
		return GNUTLS_E_INVALID_SESSION;
	}

	/* expiration check is performed inside */
	ret = gnutls_set_current_session( state, data.data, data.size);
	free(data.data);

	return 0;
}

int _gnutls_db_remove_session( GNUTLS_STATE state, uint8* session_id, int session_id_size)
{
gnutls_datum key = { session_id, session_id_size };

	return _gnutls_remove_session( state, key);
}


/* Checks if both db_store and db_retrieve functions have
 * been set up.
 */
static int _gnutls_db_func_is_ok( GNUTLS_STATE state) {
	if (state->gnutls_internals.db_store_func!=NULL &&
		state->gnutls_internals.db_retrieve_func!=NULL &&
		state->gnutls_internals.db_remove_func!=NULL) return 0;
	else return GNUTLS_E_DB_ERROR;
}



/* Stores session data to the db backend.
 */
int _gnutls_store_session( GNUTLS_STATE state, gnutls_datum session_id, gnutls_datum session_data)
{
#ifdef HAVE_LIBGDBM
GDBM_FILE dbf;
datum key = { session_id.data, session_id.size };
datum content = {session_data.data, session_data.size};
#endif
int ret = 0;

	if (state->gnutls_internals.resumable==RESUME_FALSE) 
		return GNUTLS_E_INVALID_SESSION;

	if (GNUTLS_DBNAME==NULL && _gnutls_db_func_is_ok(state)!=0) {
		return GNUTLS_E_DB_ERROR;
	}
	
	if (session_id.data==NULL || session_id.size==0)
		return GNUTLS_E_INVALID_SESSION;

	if (session_data.data==NULL || session_data.size==0)
		return GNUTLS_E_INVALID_SESSION;

	/* if we can't read why bother writing? */

#ifdef HAVE_LIBGDBM
	if (GNUTLS_DBF!=NULL) { /* use gdbm */
		dbf = gdbm_open(GNUTLS_DBNAME, 0, GDBM_WRITER, 0600, NULL);
		if (dbf==NULL) {
			/* cannot open db for writing. This may happen if multiple
			 * instances try to write. 
			 */
			return GNUTLS_E_AGAIN;
		}
		ret = gdbm_store( dbf, key, content, GDBM_INSERT);

		gdbm_close(dbf);

		return GNUTLS_E_UNIMPLEMENTED_FEATURE;
	}
	else 
#endif
		if (state->gnutls_internals.db_store_func!=NULL)
			ret = state->gnutls_internals.db_store_func( state->gnutls_internals.db_ptr, session_id, session_data);


	return (ret == 0 ? ret : GNUTLS_E_DB_ERROR);

}

/* Retrieves session data from the db backend.
 */
gnutls_datum _gnutls_retrieve_session( GNUTLS_STATE state, gnutls_datum session_id)
{
#ifdef HAVE_LIBGDBM
datum key = { session_id.data, session_id.size };
datum content;
#endif
gnutls_datum ret = { NULL, 0 };

	if (GNUTLS_DBNAME==NULL && _gnutls_db_func_is_ok(state)!=0) {
		return ret;
	}
	
	if (session_id.data==NULL || session_id.size==0)
		return ret;

	/* if we can't read why bother writing? */
#ifdef HAVE_LIBGDBM
	if (GNUTLS_DBF!=NULL) { /* use gdbm */
		content = gdbm_fetch( GNUTLS_DBF, key);
		ret.data = content.dptr;
		ret.size = content.dsize;
	} else
#endif
		if (state->gnutls_internals.db_retrieve_func!=NULL)
			ret = state->gnutls_internals.db_retrieve_func( state->gnutls_internals.db_ptr, session_id);


	return ret;

}

/* Removes session data from the db backend.
 */
int _gnutls_remove_session( GNUTLS_STATE state, gnutls_datum session_id)
{
#ifdef HAVE_LIBGDBM
GDBM_FILE dbf;
datum key = { session_id.data, session_id.size };
#endif
int ret = 0;

	if (GNUTLS_DBNAME==NULL && _gnutls_db_func_is_ok(state)!=0) {
		return GNUTLS_E_DB_ERROR;
	}
	
	if (session_id.data==NULL || session_id.size==0)
		return GNUTLS_E_INVALID_SESSION;

	/* if we can't read why bother writing? */
#ifdef HAVE_LIBGDBM
	if (GNUTLS_DBF!=NULL) { /* use gdbm */

		dbf = gdbm_open(GNUTLS_DBNAME, 0, GDBM_WRITER, 0600, NULL);
		if (dbf==NULL) {
			/* cannot open db for writing. This may happen if multiple
			 * instances try to write. 
			 */
			return GNUTLS_E_AGAIN;
		}
		ret = gdbm_delete( dbf, key);

		gdbm_close(dbf);
	} else
#endif
		if (state->gnutls_internals.db_remove_func!=NULL)
			ret = state->gnutls_internals.db_remove_func( state->gnutls_internals.db_ptr, session_id);


	return (ret == 0 ? ret : GNUTLS_E_DB_ERROR);

}