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

Store client group information in shared memory #787

Merged
merged 3 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ static bool get_client_groupids(clientsData* client)
{
char *querystr = NULL;
const char *ip = getstr(client->ippos);
client->groups = NULL;
client->found_group = false;
client->groupspos = 0u;

// Do not proceed when database is not available
if(!gravityDB_opened && !gravityDB_open())
Expand Down Expand Up @@ -257,7 +258,8 @@ static bool get_client_groupids(clientsData* client)
{
// Found no record for this client in the database
// This makes this client qualify for the special "all" group
client->groups = strdup("0");
client->groupspos = addstr("0");
client->found_group = true;
}
else
{
Expand All @@ -273,9 +275,10 @@ static bool get_client_groupids(clientsData* client)
free(querystr);
querystr = NULL;

if(client->groups != NULL)
if(client->found_group)
{
// The client is not configured through the client table, return early
// The client is not configured through the client table, we
// substituted the default group. Return early here.
return true;
}

Expand Down Expand Up @@ -327,15 +330,17 @@ static bool get_client_groupids(clientsData* client)
// There is a record for this client in the database
const char* result = (const char*)sqlite3_column_text(table_stmt, 0);
if(result != NULL)
client->groups = strdup(result);
else
client->groups = strdup("");
{
client->groupspos = addstr(result);
client->found_group = true;
}
}
else if(rc == SQLITE_DONE)
{
// Found no record for this client in the database
// -> No associated groups
client->groups = strdup("");
client->groupspos = addstr("");
client->found_group = true;
}
else
{
Expand Down Expand Up @@ -421,7 +426,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client

// Get associated groups for this client (if defined)
char *querystr = NULL;
if(client->groups == NULL && !get_client_groupids(client))
if(!client->found_group && !get_client_groupids(client))
return false;

// Prepare whitelist statement
Expand All @@ -432,7 +437,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client
// of EXISTS().
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing vw_whitelist statement for client %s", clientip);
querystr = get_client_querystr("vw_whitelist", client->groups);
querystr = get_client_querystr("vw_whitelist", getstr(client->groupspos));
sqlite3_stmt* stmt = NULL;
int rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
Expand All @@ -447,7 +452,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client
// Prepare gravity statement
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing vw_gravity statement for client %s", clientip);
querystr = get_client_querystr("vw_gravity", client->groups);
querystr = get_client_querystr("vw_gravity", getstr(client->groupspos));
rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
Expand All @@ -461,7 +466,7 @@ bool gravityDB_prepare_client_statements(const int clientID, clientsData *client
// Prepare blacklist statement
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing vw_blacklist statement for client %s", clientip);
querystr = get_client_querystr("vw_blacklist", client->groups);
querystr = get_client_querystr("vw_blacklist", getstr(client->groupspos));
rc = sqlite3_prepare_v2(gravity_db, querystr, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
Expand Down Expand Up @@ -497,12 +502,12 @@ static inline void gravityDB_finalize_client_statements(const int clientID)
gravity_stmt->set(gravity_stmt, clientID, NULL);
}

// Free group memory
// Unset group found property to trigger a check next time the
// client sends a query
clientsData* client = getClient(clientID, true);
if(client != NULL && client->groups != NULL)
if(client != NULL)
{
free(client->groups);
client->groups = NULL;
client->found_group = false;
}
}

Expand Down Expand Up @@ -888,13 +893,14 @@ bool gravityDB_get_regex_client_groups(clientsData* client, const int numregex,
gravityDB_check_fork();

char *querystr = NULL;
if(client->groups == NULL && !get_client_groupids(client))
if(!client->found_group && !get_client_groupids(client))
return false;

// Group filtering
if(asprintf(&querystr, "SELECT id from %s WHERE group_id IN (%s);", table, client->groups) < 1)
const char *groups = getstr(client->groupspos);
if(asprintf(&querystr, "SELECT id from %s WHERE group_id IN (%s);", table, groups) < 1)
{
logg("gravityDB_get_regex_client_groups(%s, %s) - asprintf() error", table, client->groups);
logg("gravityDB_get_regex_client_groups(%s, %s) - asprintf() error", table, groups);
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ int findClientID(const char *clientIP, const bool count)
// No query seen so far
client->lastQuery = 0;
client->numQueriesARP = client->count;
// Coonfigured groups are yet unknown
client->groups = NULL;
// Configured groups are yet unknown
client->found_group = false;
client->groupspos = 0u;

// Initialize client-specific overTime data
for(int i = 0; i < OVERTIME_SLOTS; i++)
Expand Down
3 changes: 2 additions & 1 deletion src/datastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ typedef struct {
typedef struct {
unsigned char magic;
bool new;
bool found_group;
int count;
int blockedcount;
int overTime[OVERTIME_SLOTS];
unsigned int numQueriesARP;
char *groups;
size_t groupspos;
size_t ippos;
size_t namepos;
time_t lastQuery;
Expand Down