Imported Upstream version 2.7
[ossec-hids.git] / src / os_dbd / rules.c
1 /* @(#) $Id: ./src/os_dbd/rules.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 __Groups_SelectGroup(char *group, DBConfig *db_config)
24  * Select group (categories) from to the db.
25  * Returns 0 if not found.
26  */
27 int __Groups_SelectGroup(char *group, 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 cat_id FROM "
38             "category WHERE cat_name = '%s'",
39             group);
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 __Groups_InsertGroup(char *group, DBConfig *db_config)
50  * Insert group (categories) in to the db.
51  */
52 int __Groups_InsertGroup(char *group, DBConfig *db_config)
53 {
54     char sql_query[OS_SIZE_1024];
55
56     memset(sql_query, '\0', OS_SIZE_1024);
57
58     /* Generating SQL */
59     snprintf(sql_query, OS_SIZE_1024 -1,
60             "INSERT INTO "
61             "category(cat_name) "
62             "VALUES ('%s')",
63             group);
64
65
66     /* Checking return code. */
67     if(!osdb_query_insert(db_config->conn, sql_query))
68     {
69         merror(DB_GENERROR, ARGV0);
70     }
71
72     return(0);
73 }
74
75
76 /** int __Groups_SelectGroupMapping()
77  * Select group (categories) from to the db.
78  * Returns 0 if not found.
79  */
80 int __Groups_SelectGroupMapping(int cat_id, int rule_id, DBConfig *db_config)
81 {
82     int result = 0;
83     char sql_query[OS_SIZE_1024];
84
85     memset(sql_query, '\0', OS_SIZE_1024);
86
87
88     /* Generating SQL */
89     snprintf(sql_query, OS_SIZE_1024 -1,
90             "SELECT id FROM signature_category_mapping "
91             "WHERE cat_id = '%u' AND rule_id = '%u'",
92             cat_id, rule_id);
93
94
95     /* Checking return code. */
96     result = osdb_query_select(db_config->conn, sql_query);
97
98     return(result);
99 }
100
101
102 /** int __Groups_InsertGroup(int cat_id, int rule_id, DBConfig *db_config)
103  * Insert group (categories) in to the db.
104  */
105 int __Groups_InsertGroupMapping(int cat_id, int rule_id, DBConfig *db_config)
106 {
107     char sql_query[OS_SIZE_1024];
108
109     memset(sql_query, '\0', OS_SIZE_1024);
110
111     /* Generating SQL */
112     snprintf(sql_query, OS_SIZE_1024 -1,
113             "INSERT INTO "
114             "signature_category_mapping(cat_id, rule_id) "
115             "VALUES ('%u', '%u')",
116             cat_id, rule_id);
117
118
119     /* Checking return code. */
120     if(!osdb_query_insert(db_config->conn, sql_query))
121     {
122         merror(DB_GENERROR, ARGV0);
123     }
124
125     return(0);
126 }
127
128
129
130 /** void _Groups_ReadInsertDB(RuleInfo *rule, DBConfig *db_config)
131  * Insert groups (categories) in to the db.
132  */
133 void _Groups_ReadInsertDB(RuleInfo *rule, DBConfig *db_config)
134 {
135     /* We must insert each group separately. */
136     int cat_id;
137     char *tmp_group;
138     char *tmp_str;
139
140
141     debug1("%s: DEBUG: entering _Groups_ReadInsertDB", ARGV0);
142
143
144     /* If group is null, just return */
145     if(rule->group == NULL)
146     {
147         return;
148     }
149
150     tmp_str = strchr(rule->group, ',');
151     tmp_group = rule->group;
152
153
154     /* Groups are separated by comma */
155     while(tmp_group)
156     {
157         if(tmp_str)
158         {
159             *tmp_str = '\0';
160             tmp_str++;
161         }
162
163         /* Removing white spaces */
164         while(*tmp_group == ' ')
165             tmp_group++;
166
167
168         /* Checking for empty group */
169         if(*tmp_group == '\0')
170         {
171             tmp_group = tmp_str;
172             if(tmp_group)
173             {
174                 tmp_str = strchr(tmp_group, ',');
175             }
176             continue;
177         }
178
179         cat_id = __Groups_SelectGroup(tmp_group, db_config);
180
181
182         /* We firt check if we have this group in the db already.
183          * If not, we add it.
184          */
185         if(cat_id == 0)
186         {
187             __Groups_InsertGroup(tmp_group, db_config);
188             cat_id = __Groups_SelectGroup(tmp_group, db_config);
189         }
190
191
192         /* If our cat_id is valid (not zero), we need to insert
193          * the mapping between the category and the rule. */
194         if(cat_id != 0)
195         {
196             /* But, we first check if the mapping is already not there. */
197             if(!__Groups_SelectGroupMapping(cat_id, rule->sigid, db_config))
198             {
199                 /* If not, we add it */
200                 __Groups_InsertGroupMapping(cat_id, rule->sigid, db_config);
201             }
202         }
203
204
205         /* Getting next category */
206         tmp_group = tmp_str;
207         if(tmp_group)
208         {
209             tmp_str = strchr(tmp_group, ',');
210         }
211     }
212
213     return;
214 }
215
216
217
218 /** void *_Rules_ReadInsertDB(RuleInfo *rule, void *db_config)
219  * Insert rules in to the db.
220  */
221 void *_Rules_ReadInsertDB(RuleInfo *rule, void *db_config)
222 {
223     DBConfig *dbc = (DBConfig *)db_config;
224     char sql_query[OS_SIZE_1024];
225     memset(sql_query, '\0', OS_SIZE_1024);
226
227
228     /* Escaping strings */
229     osdb_escapestr(rule->group);
230     osdb_escapestr(rule->comment);
231
232
233     /* Checking level limit */
234     if(rule->level > 20)
235         rule->level = 20;
236     if(rule->level < 0)
237         rule->level = 0;
238
239
240     debug1("%s: DEBUG: entering _Rules_ReadInsertDB()", ARGV0);
241
242
243     /* Checking rule limit */
244     if(rule->sigid < 0 || rule->sigid > 9999999)
245     {
246         merror("%s: Invalid rule id: %u", ARGV0, rule->sigid);
247         return(NULL);
248     }
249
250
251     /* Inserting group into the signature mapping */
252     _Groups_ReadInsertDB(rule, db_config);
253
254
255
256     debug2("%s: DEBUG: Inserting: %d", ARGV0, rule->sigid);
257
258
259     /* Generating SQL */
260     snprintf(sql_query, OS_SIZE_1024 -1,
261              "SELECT id FROM signature "
262              "where rule_id = %u",
263              rule->sigid);
264
265     if(osdb_query_select(dbc->conn, sql_query) == 0)
266     {
267         snprintf(sql_query, OS_SIZE_1024 -1,
268                 "INSERT INTO "
269                 "signature(rule_id, level, description) "
270                 "VALUES ('%u','%u','%s')",
271                 rule->sigid, rule->level, rule->comment);
272     }
273     else
274     {
275         snprintf(sql_query, OS_SIZE_1024 -1,
276                 "UPDATE signature SET level='%u',description='%s' "
277                 "WHERE rule_id='%u'",
278                 rule->level, rule->comment,rule->sigid);
279     }
280
281
282     /* Checking return code. */
283     if(!osdb_query_insert(dbc->conn, sql_query))
284     {
285         merror(DB_GENERROR, ARGV0);
286     }
287
288     return(NULL);
289 }
290
291
292 int OS_InsertRulesDB(DBConfig *db_config)
293 {
294     char **rulesfiles;
295
296     rulesfiles = db_config->includes;
297     while(rulesfiles && *rulesfiles)
298     {
299         debug1("%s: Reading rules file: '%s'", ARGV0, *rulesfiles);
300
301         if(OS_ReadXMLRules(*rulesfiles, _Rules_ReadInsertDB, db_config) < 0)
302         {
303             merror(RULES_ERROR, ARGV0, *rulesfiles);
304             return(-1);
305         }
306
307         free(*rulesfiles);
308         rulesfiles++;
309     }
310
311     free(db_config->includes);
312     db_config->includes = NULL;
313
314
315     return(0);
316 }
317
318
319 /* EOF */