summaryrefslogtreecommitdiff
path: root/src/charon/plugins/sql/sql_logger.c
blob: 6a87f9f1d2d7cf6ce26dd4dbc92746b70b8c3cb3 (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
/*
 * 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: sql_logger.c 3589 2008-03-13 14:14:44Z martin $
 */

#include <string.h>

#include "sql_logger.h"

#include <daemon.h>

typedef struct private_sql_logger_t private_sql_logger_t;

/**
 * Private data of an sql_logger_t object
 */
struct private_sql_logger_t {

	/**
	 * Public part
	 */
	sql_logger_t public;
	
	/**
	 * database connection
	 */
	database_t *db;
	
	/**
	 * logging level
	 */
	int level;
	
	/**
	 * avoid recursive logging
	 */
	bool recursive;
};


/**
 * Implementation of bus_listener_t.signal.
 */
static bool signal_(private_sql_logger_t *this, signal_t signal, level_t level,
					int thread, ike_sa_t* ike_sa, void *data,
					char *format, va_list args)
{
	if (this->recursive)
	{
		return TRUE;
	}
	this->recursive = TRUE;

	if (ike_sa && level <= this->level)
	{
		char buffer[8192];
		chunk_t local_spi, remote_spi;
		host_t *local_host, *remote_host;
		identification_t *local_id, *remote_id;
		u_int64_t ispi, rspi;
		ike_sa_id_t *id;
	
		id = ike_sa->get_id(ike_sa);
		ispi = id->get_initiator_spi(id);
		rspi = id->get_responder_spi(id);
		if (id->is_initiator(id))
		{
			local_spi.ptr = (char*)&ispi;
			remote_spi.ptr = (char*)&rspi;
		}
		else
		{
			local_spi.ptr = (char*)&rspi;
			remote_spi.ptr = (char*)&ispi;
		}
		local_spi.len = remote_spi.len = sizeof(ispi);
		local_id = ike_sa->get_my_id(ike_sa);
		remote_id = ike_sa->get_other_id(ike_sa);
		local_host = ike_sa->get_my_host(ike_sa);
		remote_host = ike_sa->get_other_host(ike_sa);
		
		vsnprintf(buffer, sizeof(buffer), format, args);
		
		this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
						  "local_spi, remote_spi, id, initiator, "
						  "local_id_type, local_id_data, "
						  "remote_id_type, remote_id_data, "
						  "host_family, local_host_data, remote_host_data) "
						  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
						  DB_BLOB, local_spi, DB_BLOB, remote_spi,
						  DB_INT, ike_sa->get_unique_id(ike_sa),
						  DB_INT, id->is_initiator(id),
						  DB_INT, local_id->get_type(local_id),
						  DB_BLOB, local_id->get_encoding(local_id),
						  DB_INT, remote_id->get_type(remote_id),
						  DB_BLOB, remote_id->get_encoding(remote_id),
						  DB_INT, local_host->get_family(local_host),
						  DB_BLOB, local_host->get_address(local_host),
						  DB_BLOB, remote_host->get_address(remote_host));
		this->db->execute(this->db, NULL, "INSERT INTO logs ("
						  "local_spi, signal, level, msg) VALUES (?, ?, ?, ?)",
						  DB_BLOB, local_spi, DB_INT, signal, DB_INT, level,
						  DB_TEXT, buffer);
	}
	this->recursive = FALSE;
	/* always stay registered */
	return TRUE;
}

/**
 * Implementation of sql_logger_t.destroy.
 */
static void destroy(private_sql_logger_t *this)
{
	free(this);
}

/**
 * Described in header.
 */
sql_logger_t *sql_logger_create(database_t *db)
{
	private_sql_logger_t *this = malloc_thing(private_sql_logger_t);
	
	this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,void*,char*,va_list))signal_;
	this->public.destroy = (void(*)(sql_logger_t*))destroy;
	
	this->db = db;
	this->recursive = FALSE;
	
	this->level = lib->settings->get_int(lib->settings,
										 "charon.plugins.sql.loglevel", -1);
	
	return &this->public;
}