1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "config/config.h"
15 static int __Groups_SelectGroup(const char *group, const DBConfig *db_config) __attribute((nonnull));
16 static int __Groups_InsertGroup(const char *group, const DBConfig *db_config) __attribute((nonnull));
17 static int __Groups_SelectGroupMapping(int cat_id, int rule_id, const DBConfig *db_config) __attribute((nonnull));
18 static int __Groups_InsertGroupMapping(int cat_id, int rule_id, const DBConfig *db_config) __attribute((nonnull));
19 static void _Groups_ReadInsertDB(RuleInfo *rule, const DBConfig *db_config) __attribute((nonnull));
20 static void *_Rules_ReadInsertDB(RuleInfo *rule, void *db_config) __attribute((nonnull));
23 /* Select group (categories) from the db
24 * Returns 0 if not found
26 static int __Groups_SelectGroup(const char *group, const DBConfig *db_config)
29 char sql_query[OS_SIZE_1024];
31 memset(sql_query, '\0', OS_SIZE_1024);
34 snprintf(sql_query, OS_SIZE_1024 - 1,
36 "category WHERE cat_name = '%s'",
39 result = osdb_query_select(db_config->conn, sql_query);
44 /* Insert group (categories) in to the db */
45 static int __Groups_InsertGroup(const char *group, const DBConfig *db_config)
47 char sql_query[OS_SIZE_1024];
49 memset(sql_query, '\0', OS_SIZE_1024);
52 snprintf(sql_query, OS_SIZE_1024 - 1,
58 if (!osdb_query_insert(db_config->conn, sql_query)) {
59 merror(DB_GENERROR, ARGV0);
65 static int __Groups_SelectGroupMapping(int cat_id, int rule_id, const DBConfig *db_config)
68 char sql_query[OS_SIZE_1024];
70 memset(sql_query, '\0', OS_SIZE_1024);
73 snprintf(sql_query, OS_SIZE_1024 - 1,
74 "SELECT id FROM signature_category_mapping "
75 "WHERE cat_id = '%u' AND rule_id = '%u'",
78 result = osdb_query_select(db_config->conn, sql_query);
83 static int __Groups_InsertGroupMapping(int cat_id, int rule_id, const DBConfig *db_config)
85 char sql_query[OS_SIZE_1024];
87 memset(sql_query, '\0', OS_SIZE_1024);
90 snprintf(sql_query, OS_SIZE_1024 - 1,
92 "signature_category_mapping(cat_id, rule_id) "
93 "VALUES ('%u', '%u')",
96 if (!osdb_query_insert(db_config->conn, sql_query)) {
97 merror(DB_GENERROR, ARGV0);
103 static void _Groups_ReadInsertDB(RuleInfo *rule, const DBConfig *db_config)
105 /* We must insert each group separately */
110 debug1("%s: DEBUG: entering _Groups_ReadInsertDB", ARGV0);
112 /* If group is null, just return */
113 if (rule->group == NULL) {
117 tmp_str = strchr(rule->group, ',');
118 tmp_group = rule->group;
120 /* Groups are separated by comma */
127 /* Remove whitespace */
128 while (*tmp_group == ' ') {
132 /* Check for empty group */
133 if (*tmp_group == '\0') {
136 tmp_str = strchr(tmp_group, ',');
141 cat_id = __Groups_SelectGroup(tmp_group, db_config);
143 /* Check if we have this group in the db already. If not, add it. */
145 __Groups_InsertGroup(tmp_group, db_config);
146 cat_id = __Groups_SelectGroup(tmp_group, db_config);
149 /* If cat_id is valid (not zero), insert the mapping between
150 * the category and the rule
153 /* First check if the mapping is not already there */
154 if (!__Groups_SelectGroupMapping(cat_id, rule->sigid, db_config)) {
155 /* If not, we add it */
156 __Groups_InsertGroupMapping(cat_id, rule->sigid, db_config);
160 /* Get next category */
163 tmp_str = strchr(tmp_group, ',');
170 /* Insert rules in to the db */
171 static void *_Rules_ReadInsertDB(RuleInfo *rule, void *db_config)
173 char sql_query[OS_SIZE_1024];
174 memset(sql_query, '\0', OS_SIZE_1024);
177 osdb_escapestr(rule->group);
178 osdb_escapestr(rule->comment);
180 /* Check level limit */
181 if (rule->level > 20) {
184 if (rule->level < 0) {
188 debug1("%s: DEBUG: entering _Rules_ReadInsertDB()", ARGV0);
190 /* Check rule limit */
191 if (rule->sigid < 0 || rule->sigid > 9999999) {
192 merror("%s: Invalid rule id: %u", ARGV0, rule->sigid);
196 /* Insert group into the signature mapping */
197 _Groups_ReadInsertDB(rule, (DBConfig *) db_config);
199 debug2("%s: DEBUG: Inserting: %d", ARGV0, rule->sigid);
202 snprintf(sql_query, OS_SIZE_1024 - 1,
204 "signature(rule_id, level, description) "
205 "VALUES ('%u','%u','%s')",
206 rule->sigid, rule->level,
207 rule->comment != NULL ? rule->comment : "NULL");
209 /* XXX We don't actually insert!?
210 if(!osdb_query_insert(dbc->conn, sql_query))
212 merror(DB_GENERROR, ARGV0);
219 int OS_InsertRulesDB(DBConfig *db_config)
223 rulesfiles = db_config->includes;
224 while (rulesfiles && *rulesfiles) {
225 debug1("%s: Reading rules file: '%s'", ARGV0, *rulesfiles);
227 if (OS_ReadXMLRules(*rulesfiles, _Rules_ReadInsertDB, db_config) < 0) {
228 merror(RULES_ERROR, ARGV0, *rulesfiles);
236 free(db_config->includes);
237 db_config->includes = NULL;