1 /* @(#) $Id: ./src/os_dbd/alert.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
18 #include "config/config.h"
23 /** int OS_SelectMaxID(DBConfig *db_config)
24 * Selects the maximum ID from the alert table.
25 * Returns 0 if not found.
27 int OS_SelectMaxID(DBConfig *db_config)
30 char sql_query[OS_SIZE_1024];
32 memset(sql_query, '\0', OS_SIZE_1024);
36 snprintf(sql_query, OS_SIZE_1024 -1,
37 "SELECT MAX(id) FROM "
38 "alert WHERE server_id = '%u'",
39 db_config->server_id);
42 /* Checking return code. */
43 result = osdb_query_select(db_config->conn, sql_query);
49 /** int __DBSelectLocation(char *locaton, DBConfig *db_config)
50 * Selects the location ID from the db.
51 * Returns 0 if not found.
53 int __DBSelectLocation(char *location, DBConfig *db_config)
56 char sql_query[OS_SIZE_1024];
58 memset(sql_query, '\0', OS_SIZE_1024);
62 snprintf(sql_query, OS_SIZE_1024 -1,
64 "location WHERE name = '%s' AND server_id = '%d' "
66 location, db_config->server_id);
69 /* Checking return code. */
70 result = osdb_query_select(db_config->conn, sql_query);
76 /** int __DBInsertLocation(char *location, DBConfig *db_config)
77 * Inserts location in to the db.
79 int __DBInsertLocation(char *location, DBConfig *db_config)
81 char sql_query[OS_SIZE_1024];
83 memset(sql_query, '\0', OS_SIZE_1024);
86 snprintf(sql_query, OS_SIZE_1024 -1,
88 "location(server_id, name) "
89 "VALUES ('%u', '%s')",
90 db_config->server_id, location);
93 /* Checking return code. */
94 if(!osdb_query_insert(db_config->conn, sql_query))
96 merror(DB_GENERROR, ARGV0);
104 /** int OS_Alert_InsertDB(DBConfig *db_config)
105 * Insert alert into to the db.
106 * Returns 1 on success or 0 on error.
108 int OS_Alert_InsertDB(alert_data *al_data, DBConfig *db_config)
111 unsigned int s_ip = 0, d_ip = 0, location_id = 0;
112 unsigned short s_port = 0, d_port = 0;
114 char sql_query[OS_SIZE_8192 +1];
115 char *fulllog = NULL;
118 /* Clearing the memory before insert */
120 sql_query[OS_SIZE_8192] = '\0';
123 /* Converting srcip to int */
128 /* Extracting ip address */
129 if(inet_aton(al_data->srcip, &net))
135 /* Converting dstip to int */
140 /* Extracting ip address */
141 if(inet_aton(al_data->dstip, &net))
148 s_port = al_data->srcport;
150 /* Destination Port */
151 d_port = al_data->dstport;
154 /* Escaping strings */
155 osdb_escapestr(al_data->user);
158 /* We first need to insert the location */
159 loc_id = OSHash_Get(db_config->location_hash, al_data->location);
162 /* If we dont have location id, we must select and/or insert in the db */
165 location_id = __DBSelectLocation(al_data->location, db_config);
169 __DBInsertLocation(al_data->location, db_config);
170 location_id = __DBSelectLocation(al_data->location, db_config);
175 merror("%s: Unable to insert location: '%s'.",
176 ARGV0, al_data->location);
182 os_calloc(1, sizeof(int), loc_id);
183 *loc_id = location_id;
184 OSHash_Add(db_config->location_hash, al_data->location, loc_id);
189 while(al_data->log[i])
191 long len = strlen(al_data->log[i]);
193 if (al_data->log[i+1]) {
194 snprintf(templog, len+2, "%s\n", al_data->log[i]);
197 snprintf(templog, len+1, "%s", al_data->log[i]);
199 fulllog = os_LoadString(fulllog, templog);
200 // fulllog = os_LoadString(fulllog, al_data->log[i]);
203 osdb_escapestr(fulllog);
204 if(strlen(fulllog) > 7456)
208 fulllog[7456] = '\0';
213 if(db_config->db_type == POSTGDB)
215 /* On postgres we need to escape the user field. */
216 snprintf(sql_query, OS_SIZE_8192,
218 "data(id, server_id, \"user\", full_log) "
219 "VALUES ('%u', '%u', '%s', '%s') ",
220 db_config->alert_id, db_config->server_id,
221 al_data->user, fulllog);
225 snprintf(sql_query, OS_SIZE_8192,
227 "data(id, server_id, user, full_log) "
228 "VALUES ('%u', '%u', '%s', '%s') ",
229 db_config->alert_id, db_config->server_id,
230 al_data->user, fulllog);
237 /* Inserting into the db */
238 if(!osdb_query_insert(db_config->conn, sql_query))
240 merror(DB_GENERROR, ARGV0);
245 /* Generating final SQL */
246 snprintf(sql_query, OS_SIZE_8192,
248 "alert(id,server_id,rule_id,timestamp,location_id,src_ip,src_port,dst_ip,dst_port,alertid) "
249 "VALUES ('%u', '%u', '%u','%u', '%u', '%lu', '%u', '%lu', '%u', '%s')",
250 db_config->alert_id, db_config->server_id, al_data->rule,
251 (unsigned int)time(0), *loc_id,
252 (unsigned long)ntohl(s_ip), (unsigned short)s_port,
253 (unsigned long)ntohl(d_ip), (unsigned short)d_port,
257 /* Inserting into the db */
258 if(!osdb_query_insert(db_config->conn, sql_query))
260 merror(DB_GENERROR, ARGV0);
264 db_config->alert_id++;