/* Select the maximum ID from the alert table * Returns 0 if not found */ int OS_SelectMaxID(const DBConfig *db_config) { int result = 0; char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Generate SQL */ snprintf(sql_query, OS_SIZE_1024 - 1, "SELECT MAX(id) FROM " "alert WHERE server_id = '%u'", db_config->server_id); result = osdb_query_select(db_config->conn, sql_query); return (result); }
/** int __DBInsertServer(char *server, char *info, DBConfig *db_config) * Inserts server in to the db. */ int __DBInsertServer(char *server, char *info, DBConfig *db_config) { char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Checking if the server is present */ snprintf(sql_query, OS_SIZE_1024 -1, "SELECT id from server where hostname = '%s'", server); /* If not present, we insert */ if(osdb_query_select(db_config->conn, sql_query) == 0) { snprintf(sql_query, OS_SIZE_1024 -1, "INSERT INTO " "server(last_contact, version, hostname, information) " "VALUES ('%u', '%s', '%s', '%s')", (unsigned int)time(0), __version, server, info); /* Checking return code. */ if(!osdb_query_insert(db_config->conn, sql_query)) { merror(DB_GENERROR, ARGV0); } } /* If it is, we update it */ else { snprintf(sql_query, OS_SIZE_1024 -1, "UPDATE server SET " "last_contact='%u',version='%s',information='%s' " "WHERE hostname = '%s'", (unsigned int)time(0), __version, info, server); /* Checking return code. */ if(!osdb_query_insert(db_config->conn, sql_query)) { merror(DB_GENERROR, ARGV0); } } return(0); }
/* Select the server ID from the db * Returns 0 if not found */ static int __DBSelectServer(const char *server, const DBConfig *db_config) { int result = 0; char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Generate SQL */ snprintf(sql_query, OS_SIZE_1024 - 1, "SELECT id FROM " "server WHERE hostname = '%s'", server); result = osdb_query_select(db_config->conn, sql_query); return (result); }
/* Select the location ID from the db * Returns 0 if not found */ static int __DBSelectLocation(const char *location, const DBConfig *db_config) { int result = 0; char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Generate SQL */ snprintf(sql_query, OS_SIZE_1024 - 1, "SELECT id FROM " "location WHERE name = '%s' AND server_id = '%d' " "LIMIT 1", location, db_config->server_id); result = osdb_query_select(db_config->conn, sql_query); return (result); }
/** int __Groups_SelectGroupMapping() * Select group (categories) from to the db. * Returns 0 if not found. */ int __Groups_SelectGroupMapping(int cat_id, int rule_id, DBConfig *db_config) { int result = 0; char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Generating SQL */ snprintf(sql_query, OS_SIZE_1024 -1, "SELECT id FROM signature_category_mapping " "WHERE cat_id = '%u' AND rule_id = '%u'", cat_id, rule_id); /* Checking return code. */ result = osdb_query_select(db_config->conn, sql_query); return(result); }
/** int __Groups_SelectGroup(char *group, DBConfig *db_config) * Select group (categories) from to the db. * Returns 0 if not found. */ int __Groups_SelectGroup(char *group, DBConfig *db_config) { int result = 0; char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Generating SQL */ snprintf(sql_query, OS_SIZE_1024 -1, "SELECT cat_id FROM " "category WHERE cat_name = '%s'", group); /* Checking return code. */ result = osdb_query_select(db_config->conn, sql_query); return(result); }
/** void *_Rules_ReadInsertDB(RuleInfo *rule, void *db_config) * Insert rules in to the db. */ void *_Rules_ReadInsertDB(RuleInfo *rule, void *db_config) { DBConfig *dbc = (DBConfig *)db_config; char sql_query[OS_SIZE_1024]; memset(sql_query, '\0', OS_SIZE_1024); /* Escaping strings */ osdb_escapestr(rule->group); osdb_escapestr(rule->comment); /* Checking level limit */ if(rule->level > 20) rule->level = 20; if(rule->level < 0) rule->level = 0; debug1("%s: DEBUG: entering _Rules_ReadInsertDB()", ARGV0); /* Checking rule limit */ if(rule->sigid < 0 || rule->sigid > 9999999) { merror("%s: Invalid rule id: %u", ARGV0, rule->sigid); return(NULL); } /* Inserting group into the signature mapping */ _Groups_ReadInsertDB(rule, db_config); debug2("%s: DEBUG: Inserting: %d", ARGV0, rule->sigid); /* Generating SQL */ snprintf(sql_query, OS_SIZE_1024 -1, "SELECT id FROM signature " "where rule_id = %u", rule->sigid); if(osdb_query_select(dbc->conn, sql_query) == 0) { snprintf(sql_query, OS_SIZE_1024 -1, "INSERT INTO " "signature(rule_id, level, description) " "VALUES ('%u','%u','%s')", rule->sigid, rule->level, rule->comment); } else { snprintf(sql_query, OS_SIZE_1024 -1, "UPDATE signature SET level='%u',description='%s' " "WHERE rule_id='%u'", rule->level, rule->comment,rule->sigid); } /* Checking return code. */ if(!osdb_query_insert(dbc->conn, sql_query)) { merror(DB_GENERROR, ARGV0); } return(NULL); }