a55777e3fe72cfb0eee853607b037d70f140f52e
[strongswan.git] / src / charon / plugins / sql / sql_logger.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id: sql_logger.c 3589 2008-03-13 14:14:44Z martin $
16 */
17
18 #include <string.h>
19
20 #include "sql_logger.h"
21
22 #include <daemon.h>
23
24 typedef struct private_sql_logger_t private_sql_logger_t;
25
26 /**
27 * Private data of an sql_logger_t object
28 */
29 struct private_sql_logger_t {
30
31 /**
32 * Public part
33 */
34 sql_logger_t public;
35
36 /**
37 * database connection
38 */
39 database_t *db;
40
41 /**
42 * logging level
43 */
44 int level;
45 };
46
47
48 /**
49 * Implementation of bus_listener_t.signal.
50 */
51 static bool signal_(private_sql_logger_t *this, signal_t signal, level_t level,
52 int thread, ike_sa_t* ike_sa, char *format, va_list args)
53 {
54 if (ike_sa && level <= this->level)
55 {
56 char buffer[8192], local_id[64], remote_id[64], local[40], remote[40];
57 char *current = buffer, *next;
58 chunk_t local_spi, remote_spi;
59 u_int64_t ispi, rspi;
60 bool initiator;
61 ike_sa_id_t *id;
62
63 id = ike_sa->get_id(ike_sa);
64 initiator = id->is_initiator(id);
65 ispi = id->get_initiator_spi(id);
66 rspi = id->get_responder_spi(id);
67 if (initiator)
68 {
69 local_spi.ptr = (char*)&ispi;
70 remote_spi.ptr = (char*)&rspi;
71 }
72 else
73 {
74 local_spi.ptr = (char*)&rspi;
75 remote_spi.ptr = (char*)&ispi;
76 }
77 local_spi.len = remote_spi.len = sizeof(ispi);
78 snprintf(local_id, sizeof(local_id), "%D", ike_sa->get_my_id(ike_sa));
79 snprintf(remote_id, sizeof(remote_id), "%D", ike_sa->get_other_id(ike_sa));
80 snprintf(local, sizeof(local), "%H", ike_sa->get_my_host(ike_sa));
81 snprintf(remote, sizeof(remote), "%H", ike_sa->get_other_host(ike_sa));
82
83 /* write in memory buffer first */
84 vsnprintf(buffer, sizeof(buffer), format, args);
85
86 this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
87 "local_spi, remote_spi, id, initiator, "
88 "local_id, remote_id, local, remote) "
89 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
90 DB_BLOB, local_spi, DB_BLOB, remote_spi,
91 DB_INT, ike_sa->get_unique_id(ike_sa),
92 DB_INT, initiator,
93 DB_TEXT, local_id, DB_TEXT, remote_id,
94 DB_TEXT, local, DB_TEXT, remote);
95 /* do a log with every line */
96 while (current)
97 {
98 next = strchr(current, '\n');
99 if (next)
100 {
101 *(next++) = '\0';
102 }
103 this->db->execute(this->db, NULL,
104 "INSERT INTO logs (local_spi, signal, level, msg) "
105 "VALUES (?, ?, ?, ?)",
106 DB_BLOB, local_spi, DB_INT, signal, DB_INT, level,
107 DB_TEXT, current);
108 current = next;
109 }
110 }
111 /* always stay registered */
112 return TRUE;
113 }
114
115 /**
116 * Implementation of sql_logger_t.destroy.
117 */
118 static void destroy(private_sql_logger_t *this)
119 {
120 free(this);
121 }
122
123 /**
124 * Described in header.
125 */
126 sql_logger_t *sql_logger_create(database_t *db)
127 {
128 private_sql_logger_t *this = malloc_thing(private_sql_logger_t);
129
130 this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
131 this->public.destroy = (void(*)(sql_logger_t*))destroy;
132
133 this->db = db;
134
135 this->level = lib->settings->get_int(lib->settings,
136 "charon.plugins.sql.loglevel", 1);
137
138 return &this->public;
139 }
140