Imported Upstream version 2.7
[ossec-hids.git] / src / os_dbd / alert.c
1 /* @(#) $Id: ./src/os_dbd/alert.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "dbd.h"
18 #include "config/config.h"
19 #include "rules_op.h"
20
21
22
23 /** int OS_SelectMaxID(DBConfig *db_config)
24  * Selects the maximum ID from the alert table.
25  * Returns 0 if not found.
26  */
27 int OS_SelectMaxID(DBConfig *db_config)
28 {
29     int result = 0;
30     char sql_query[OS_SIZE_1024];
31
32     memset(sql_query, '\0', OS_SIZE_1024);
33
34
35     /* Generating SQL */
36     snprintf(sql_query, OS_SIZE_1024 -1,
37             "SELECT MAX(id) FROM "
38             "alert WHERE server_id = '%u'",
39             db_config->server_id);
40
41
42     /* Checking return code. */
43     result = osdb_query_select(db_config->conn, sql_query);
44
45     return(result);
46 }
47
48
49 /** int __DBSelectLocation(char *locaton, DBConfig *db_config)
50  * Selects the location ID from the db.
51  * Returns 0 if not found.
52  */
53 int __DBSelectLocation(char *location, DBConfig *db_config)
54 {
55     int result = 0;
56     char sql_query[OS_SIZE_1024];
57
58     memset(sql_query, '\0', OS_SIZE_1024);
59
60
61     /* Generating SQL */
62     snprintf(sql_query, OS_SIZE_1024 -1,
63             "SELECT id FROM "
64             "location WHERE name = '%s' AND server_id = '%d' "
65             "LIMIT 1",
66             location, db_config->server_id);
67
68
69     /* Checking return code. */
70     result = osdb_query_select(db_config->conn, sql_query);
71
72     return(result);
73 }
74
75
76 /** int __DBInsertLocation(char *location, DBConfig *db_config)
77  * Inserts location in to the db.
78  */
79 int __DBInsertLocation(char *location, DBConfig *db_config)
80 {
81     char sql_query[OS_SIZE_1024];
82
83     memset(sql_query, '\0', OS_SIZE_1024);
84
85     /* Generating SQL */
86     snprintf(sql_query, OS_SIZE_1024 -1,
87             "INSERT INTO "
88             "location(server_id, name) "
89             "VALUES ('%u', '%s')",
90             db_config->server_id, location);
91
92
93     /* Checking return code. */
94     if(!osdb_query_insert(db_config->conn, sql_query))
95     {
96         merror(DB_GENERROR, ARGV0);
97     }
98
99     return(0);
100 }
101
102
103
104 /** int OS_Alert_InsertDB(DBConfig *db_config)
105  * Insert alert into to the db.
106  * Returns 1 on success or 0 on error.
107  */
108 int OS_Alert_InsertDB(alert_data *al_data, DBConfig *db_config)
109 {
110     int i;
111     unsigned int s_ip = 0, d_ip = 0, location_id = 0;
112     unsigned short s_port = 0, d_port = 0;
113     int *loc_id;
114     char sql_query[OS_SIZE_8192 +1];
115     char *fulllog = NULL;
116
117
118     /* Clearing the memory before insert */
119     sql_query[0] = '\0';
120     sql_query[OS_SIZE_8192] = '\0';
121
122
123     /* Converting srcip to int */
124     if(al_data->srcip)
125     {
126         struct in_addr net;
127
128         /* Extracting ip address */
129         if(inet_aton(al_data->srcip, &net))
130         {
131             s_ip = net.s_addr;
132         }
133     }
134
135     /* Converting dstip to int */
136     if(al_data->dstip)
137     {
138         struct in_addr net;
139
140         /* Extracting ip address */
141         if(inet_aton(al_data->dstip, &net))
142         {
143             d_ip = net.s_addr;
144         }
145     }
146
147     /* Source Port */
148     s_port = al_data->srcport;
149
150     /* Destination Port */
151     d_port = al_data->dstport;
152
153
154     /* Escaping strings */
155     osdb_escapestr(al_data->user);
156
157
158     /* We first need to insert the location */
159     loc_id = OSHash_Get(db_config->location_hash, al_data->location);
160
161
162     /* If we dont have location id, we must select and/or insert in the db */
163     if(!loc_id)
164     {
165         location_id = __DBSelectLocation(al_data->location, db_config);
166         if(location_id == 0)
167         {
168             /* Insert it */
169             __DBInsertLocation(al_data->location, db_config);
170             location_id = __DBSelectLocation(al_data->location, db_config);
171         }
172
173         if(!location_id)
174         {
175             merror("%s: Unable to insert location: '%s'.",
176                    ARGV0, al_data->location);
177             return(0);
178         }
179
180
181         /* Adding to hash */
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);
185     }
186
187
188     i = 0;
189     while(al_data->log[i])
190     {
191         long len = strlen(al_data->log[i]);
192         char templog[len+2];
193         if (al_data->log[i+1]) {
194             snprintf(templog, len, "%s\n", al_data->log[i]);
195         }
196         else {
197             snprintf(templog, len, "%s", al_data->log[i]);
198         }
199         fulllog = os_LoadString(fulllog, templog);
200 //      fulllog = os_LoadString(fulllog, al_data->log[i]);
201         i++;
202     }
203     osdb_escapestr(fulllog);
204     if(strlen(fulllog) >  7456)
205     {
206         fulllog[7454] = '.';
207         fulllog[7455] = '.';
208         fulllog[7456] = '\0';
209     }
210
211
212     /* Inserting data */
213     if(db_config->db_type == POSTGDB)
214     {
215         /* On postgres we need to escape the user field. */
216         snprintf(sql_query, OS_SIZE_8192,
217                 "INSERT INTO "
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);
222     }
223     else
224     {
225         snprintf(sql_query, OS_SIZE_8192,
226                 "INSERT INTO "
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);
231     }
232
233     free(fulllog);
234     fulllog = NULL;
235
236
237     /* Inserting into the db */
238     if(!osdb_query_insert(db_config->conn, sql_query))
239     {
240         merror(DB_GENERROR, ARGV0);
241     }
242
243
244
245     /* Generating final SQL */
246     snprintf(sql_query, OS_SIZE_8192,
247             "INSERT INTO "
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,
254             al_data->alertid);
255
256
257     /* Inserting into the db */
258     if(!osdb_query_insert(db_config->conn, sql_query))
259     {
260         merror(DB_GENERROR, ARGV0);
261     }
262
263
264     db_config->alert_id++;
265     return(1);
266 }
267
268
269 /* EOF */