Imported Upstream version 2.3
[ossec-hids.git] / src / os_dbd / alert.c
1 /* @(#) $Id: alert.c,v 1.8 2009/06/24 17:06:29 dcid Exp $ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 3) as published by the FSF - Free Software
9  * Foundation
10  *
11  * License details at the LICENSE file included with OSSEC or
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16 #include "dbd.h"
17 #include "config/config.h"
18 #include "rules_op.h"
19
20
21
22 /** int OS_SelectMaxID(DBConfig *db_config)
23  * Selects the maximum ID from the alert table.
24  * Returns 0 if not found.
25  */
26 int OS_SelectMaxID(DBConfig *db_config)
27 {
28     int result = 0;
29     char sql_query[OS_SIZE_1024];
30
31     memset(sql_query, '\0', OS_SIZE_1024);
32
33
34     /* Generating SQL */
35     snprintf(sql_query, OS_SIZE_1024 -1,
36             "SELECT MAX(id) FROM "
37             "alert WHERE server_id = '%u'",
38             db_config->server_id);
39
40
41     /* Checking return code. */
42     result = osdb_query_select(db_config->conn, sql_query);
43
44     return(result);
45 }
46
47
48 /** int __DBSelectLocation(char *locaton, DBConfig *db_config)
49  * Selects the location ID from the db.
50  * Returns 0 if not found.
51  */
52 int __DBSelectLocation(char *location, DBConfig *db_config)
53 {
54     int result = 0;
55     char sql_query[OS_SIZE_1024];
56
57     memset(sql_query, '\0', OS_SIZE_1024);
58
59
60     /* Generating SQL */
61     snprintf(sql_query, OS_SIZE_1024 -1,
62             "SELECT id FROM "
63             "location WHERE name = '%s' AND server_id = '%d' "
64             "LIMIT 1",
65             location, db_config->server_id);
66
67
68     /* Checking return code. */
69     result = osdb_query_select(db_config->conn, sql_query);
70
71     return(result);
72 }
73
74
75 /** int __DBInsertLocation(char *location, DBConfig *db_config)
76  * Inserts location in to the db.
77  */
78 int __DBInsertLocation(char *location, DBConfig *db_config)
79 {
80     char sql_query[OS_SIZE_1024];
81     
82     memset(sql_query, '\0', OS_SIZE_1024);
83
84     /* Generating SQL */
85     snprintf(sql_query, OS_SIZE_1024 -1,
86             "INSERT INTO "
87             "location(server_id, name) "
88             "VALUES ('%u', '%s')",
89             db_config->server_id, location);
90
91
92     /* Checking return code. */
93     if(!osdb_query_insert(db_config->conn, sql_query))
94     {
95         merror(DB_GENERROR, ARGV0);
96     }
97
98     return(0);
99 }
100
101
102
103 /** int OS_Alert_InsertDB(DBConfig *db_config)
104  * Insert alert into to the db.
105  * Returns 1 on success or 0 on error.
106  */
107 int OS_Alert_InsertDB(alert_data *al_data, DBConfig *db_config)
108 {
109     unsigned int s_ip = 0, d_ip = 0, location_id = 0;
110     int *loc_id;
111     char sql_query[OS_SIZE_2048 +1];
112
113
114     /* Clearing the memory before insert */
115     memset(sql_query, '\0', OS_SIZE_2048 +1);
116     
117
118     /* Converting srcip to int */
119     if(al_data->srcip)
120     {
121         struct in_addr net;
122
123         /* Extracting ip address */
124         if(inet_aton(al_data->srcip, &net))
125         {
126             s_ip = net.s_addr;
127         }
128     }
129     d_ip = 0;
130
131
132     /* Escaping strings */
133     osdb_escapestr(al_data->user);
134     osdb_escapestr(al_data->log[0]);
135
136
137     /* We first need to insert the location */
138     loc_id = OSHash_Get(db_config->location_hash, al_data->location);
139     
140     
141     /* If we dont have location id, we must select and/or insert in the db */
142     if(!loc_id)
143     {
144         location_id = __DBSelectLocation(al_data->location, db_config);
145         if(location_id == 0)
146         {
147             /* Insert it */
148             __DBInsertLocation(al_data->location, db_config);
149             location_id = __DBSelectLocation(al_data->location, db_config);
150         }
151
152         if(!location_id)
153         {
154             merror("%s: Unable to insert location: '%s'.", 
155                    ARGV0, al_data->location);
156             return(0);
157         }
158
159
160         /* Adding to hash */
161         os_calloc(1, sizeof(int), loc_id);
162         *loc_id = location_id;
163         OSHash_Add(db_config->location_hash, al_data->location, loc_id);
164     }
165     
166
167     /* Inserting data */
168     if(db_config->db_type == POSTGDB)
169     {
170         /* On postgres we need to escape the user field. */
171         snprintf(sql_query, OS_SIZE_2048,
172                 "INSERT INTO "
173                 "data(id, server_id, \"user\", full_log) "
174                 "VALUES ('%u', '%u', '%s', '%s') ",
175                 db_config->alert_id, db_config->server_id, 
176                 al_data->user, al_data->log[0]);
177     }
178     else
179     {
180         snprintf(sql_query, OS_SIZE_2048,
181                 "INSERT INTO "
182                 "data(id, server_id, user, full_log) "
183                 "VALUES ('%u', '%u', '%s', '%s') ",
184                 db_config->alert_id, db_config->server_id, 
185                 al_data->user, al_data->log[0]);
186     }
187     
188     
189     /* Inserting into the db */
190     if(!osdb_query_insert(db_config->conn, sql_query))
191     {
192         merror(DB_GENERROR, ARGV0);
193     }
194                                 
195
196
197     /* Generating final SQL */
198     snprintf(sql_query, OS_SIZE_2048,
199             "INSERT INTO "
200             "alert(id,server_id,rule_id,timestamp,location_id,src_ip) "
201             "VALUES ('%u', '%u', '%u','%u', '%u', '%lu')",
202             db_config->alert_id, db_config->server_id, al_data->rule,
203             (unsigned int)time(0), *loc_id, (unsigned long)ntohl(s_ip));
204
205
206     /* Inserting into the db */
207     if(!osdb_query_insert(db_config->conn, sql_query))
208     {
209         merror(DB_GENERROR, ARGV0);
210     }
211
212     
213     db_config->alert_id++;
214     return(1);
215 }
216
217
218 /* EOF */