Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Ensure each regex is imported only once" #690

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/database/gravity-db.c
Expand Up @@ -345,9 +345,7 @@ bool gravityDB_getTable(const unsigned char list)

char *querystr = NULL;
// Build correct query string to be used depending on list to be read
// We GROUP BY id as the view also includes the group_id leading to possible duplicates
// when domains are included in more than one group
if(asprintf(&querystr, "SELECT domain, id FROM %s GROUP BY id", tablename[list]) < 18)
if(asprintf(&querystr, "SELECT domain, id FROM %s", tablename[list]) < 18)
{
logg("readGravity(%u) - asprintf() error", list);
return false;
Expand Down Expand Up @@ -612,8 +610,8 @@ bool gravityDB_get_regex_client_groups(clientsData* client, const int numregex,
}

// Perform query
if(config.debug & DEBUG_REGEX)
logg("Regex %s: Querying groups for client %s: \"%s\"", regextype[type], getstr(client->ippos), querystr);
if(config.debug & DEBUG_DATABASE)
logg("Querying regex groups: %s", querystr);
while((rc = sqlite3_step(query_stmt)) == SQLITE_ROW)
{
const int result = sqlite3_column_int(query_stmt, 0);
Expand All @@ -625,8 +623,6 @@ bool gravityDB_get_regex_client_groups(clientsData* client, const int numregex,
if(type == REGEX_WHITELIST)
regexID += counters->num_regex[REGEX_BLACKLIST];
set_per_client_regex(clientID, regexID, true);
if(config.debug & DEBUG_REGEX)
logg("Regex %s: Enabling regex with DB ID %i for client %s", regextype[type], regexid[i], getstr(client->ippos));
break;
}
}
Expand Down
43 changes: 10 additions & 33 deletions src/regex.c
Expand Up @@ -28,7 +28,7 @@ static bool *regex_available[2] = { NULL };
static int *regex_id[2] = { NULL };
static char **regexbuffer[2] = { NULL };

const char *regextype[] = { "blacklist", "whitelist" };
static const char *regextype[] = { "blacklist", "whitelist" };

static void log_regex_error(const int errcode, const int index, const unsigned char regexid, const char *regexin)
{
Expand Down Expand Up @@ -76,9 +76,7 @@ bool match_regex(const char *input, const int clientID, const unsigned char rege
if(!regex_available[regexid][index])
{
if(config.debug & DEBUG_REGEX)
logg("Regex %s (DB ID %d) \"%s\" is NOT AVAILABLE",
regextype[regexid], regex_id[regexid][index],
regexbuffer[regexid][index]);
logg("Regex %s ID %d not available", regextype[regexid], index);

continue;
}
Expand All @@ -90,12 +88,7 @@ bool match_regex(const char *input, const int clientID, const unsigned char rege
if(!get_per_client_regex(clientID, regexID))
{
if(config.debug & DEBUG_REGEX)
{
clientsData* client = getClient(clientID, true);
logg("Regex %s (DB ID %d) \"%s\" NOT ENABLED for client %s",
regextype[regexid], regex_id[regexid][index],
regexbuffer[regexid][index], getstr(client->ippos));
}
logg("Regex %s ID %d not enabled for this client", regextype[regexid], index);

continue;
}
Expand All @@ -111,21 +104,9 @@ bool match_regex(const char *input, const int clientID, const unsigned char rege

// Print match message when in regex debug mode
if(config.debug & DEBUG_REGEX)
{
logg("Regex %s (DB ID %i) >> MATCH: \"%s\" vs. \"%s\"",
regextype[regexid], regex_id[regexid][index],
input, regexbuffer[regexid][index]);
}
logg("Regex %s (ID %i) \"%s\" matches \"%s\"", regextype[regexid], regex_id[regexid][index], regexbuffer[regexid][index], input);
break;
}

// Print no match message when in regex debug mode
if(config.debug & DEBUG_REGEX && !matched)
{
logg("Regex %s (DB ID %i) NO match: \"%s\" vs. \"%s\"",
regextype[regexid], regex_id[regexid][index],
input, regexbuffer[regexid][index]);
}
}

double elapsed = timer_elapsed_msec(REGEX_TIMER);
Expand Down Expand Up @@ -202,11 +183,11 @@ void allocate_regex_client_enabled(clientsData *client, const int clientID)

static void read_regex_table(const unsigned char regexid)
{
// Get table ID
unsigned char tableID = (regexid == REGEX_BLACKLIST) ? REGEX_BLACKLIST_TABLE : REGEX_WHITELIST_TABLE;
// Get database ID
unsigned char databaseID = (regexid == REGEX_BLACKLIST) ? REGEX_BLACKLIST_TABLE : REGEX_WHITELIST_TABLE;

// Get number of lines in the regex table
counters->num_regex[regexid] = gravityDB_count(tableID);
counters->num_regex[regexid] = gravityDB_count(databaseID);

if(counters->num_regex[regexid] == 0)
{
Expand All @@ -215,7 +196,7 @@ static void read_regex_table(const unsigned char regexid)
}
else if(counters->num_regex[regexid] == DB_FAILED)
{
logg("WARN: Database query failed, assuming there are no %s regex entries", regextype[regexid]);
logg("WARN: Database query failed, assuming there are no regex %s entries", regextype[regexid]);
counters->num_regex[regexid] = 0;
return;
}
Expand All @@ -230,9 +211,9 @@ static void read_regex_table(const unsigned char regexid)
regexbuffer[regexid] = calloc(counters->num_regex[regexid], sizeof(char*));

// Connect to regex table
if(!gravityDB_getTable(tableID))
if(!gravityDB_getTable(databaseID))
{
logg("read_regex_from_database(): Error getting %s regex table from database", regextype[regexid]);
logg("read_regex_from_database(): Error getting regex %s table from database", regextype[regexid]);
return;
}

Expand All @@ -256,10 +237,6 @@ static void read_regex_table(const unsigned char regexid)
continue;

// Compile this regex
if(config.debug & DEBUG_REGEX)
{
logg("Compiling %s regex %i (database ID %i): %s", regextype[regexid], i, rowid, domain);
}
regex_available[regexid][i] = compile_regex(domain, i, regexid);
regex_id[regexid][i] = rowid;

Expand Down
1 change: 0 additions & 1 deletion src/regex_r.h
Expand Up @@ -13,7 +13,6 @@
// clientsData type
#include "datastructure.h"

extern const char *regextype[];

bool match_regex(const char *input, const int clientID, const unsigned char regexid);
void allocate_regex_client_enabled(clientsData *client, const int clientID);
Expand Down