517b65c477f78c2a3a240f9cadd3f5e241a4bf3a
[ossec-hids.git] / src / os_dbd / alert.c
1 /* @(#) $Id$ */
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 2) 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     int i;
110     unsigned int s_ip = 0, d_ip = 0, location_id = 0;
111     int *loc_id;
112     char sql_query[OS_SIZE_8192 +1];
113     char *fulllog = NULL;
114
115
116     /* Clearing the memory before insert */
117     sql_query[0] = '\0';
118     sql_query[OS_SIZE_8192] = '\0';
119     
120
121     /* Converting srcip to int */
122     if(al_data->srcip)
123     {
124         struct in_addr net;
125
126         /* Extracting ip address */
127         if(inet_aton(al_data->srcip, &net))
128         {
129             s_ip = net.s_addr;
130         }
131     }
132     d_ip = 0;
133
134
135     /* Escaping strings */
136     osdb_escapestr(al_data->user);
137
138
139     /* We first need to insert the location */
140     loc_id = OSHash_Get(db_config->location_hash, al_data->location);
141     
142     
143     /* If we dont have location id, we must select and/or insert in the db */
144     if(!loc_id)
145     {
146         location_id = __DBSelectLocation(al_data->location, db_config);
147         if(location_id == 0)
148         {
149             /* Insert it */
150             __DBInsertLocation(al_data->location, db_config);
151             location_id = __DBSelectLocation(al_data->location, db_config);
152         }
153
154         if(!location_id)
155         {
156             merror("%s: Unable to insert location: '%s'.", 
157                    ARGV0, al_data->location);
158             return(0);
159         }
160
161
162         /* Adding to hash */
163         os_calloc(1, sizeof(int), loc_id);
164         *loc_id = location_id;
165         OSHash_Add(db_config->location_hash, al_data->location, loc_id);
166     }
167     
168
169     i = 0;
170     while(al_data->log[i])
171     {
172         fulllog = os_LoadString(fulllog, al_data->log[i]);
173         i++;
174     }
175     osdb_escapestr(fulllog);
176
177
178     /* Inserting data */
179     if(db_config->db_type == POSTGDB)
180     {
181         /* On postgres we need to escape the user field. */
182         snprintf(sql_query, OS_SIZE_8192,
183                 "INSERT INTO "
184                 "data(id, server_id, \"user\", full_log) "
185                 "VALUES ('%u', '%u', '%s', '%s') ",
186                 db_config->alert_id, db_config->server_id, 
187                 al_data->user, fulllog);
188     }
189     else
190     {
191         snprintf(sql_query, OS_SIZE_8192,
192                 "INSERT INTO "
193                 "data(id, server_id, user, full_log) "
194                 "VALUES ('%u', '%u', '%s', '%s') ",
195                 db_config->alert_id, db_config->server_id, 
196                 al_data->user, fulllog);
197     }
198
199     free(fulllog);
200     fulllog = NULL;
201     
202     
203     /* Inserting into the db */
204     if(!osdb_query_insert(db_config->conn, sql_query))
205     {
206         merror(DB_GENERROR, ARGV0);
207     }
208                                 
209
210
211     /* Generating final SQL */
212     snprintf(sql_query, OS_SIZE_8192,
213             "INSERT INTO "
214             "alert(id,server_id,rule_id,timestamp,location_id,src_ip) "
215             "VALUES ('%u', '%u', '%u','%u', '%u', '%lu')",
216             db_config->alert_id, db_config->server_id, al_data->rule,
217             (unsigned int)time(0), *loc_id, (unsigned long)ntohl(s_ip));
218
219
220     /* Inserting into the db */
221     if(!osdb_query_insert(db_config->conn, sql_query))
222     {
223         merror(DB_GENERROR, ARGV0);
224     }
225
226     
227     db_config->alert_id++;
228     return(1);
229 }
230
231
232 /* EOF */