Greenbone Vulnerability Management Libraries  10.0.0
Data Structures | Macros | Functions | Variables
kb.c File Reference

Knowledge base management API - Redis backend. More...

#include "kb.h"
#include <errno.h>
#include <glib.h>
#include <hiredis/hiredis.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Data Structures

struct  kb_redis
 Subclass of struct kb, it contains the redis-specific fields, such as the redis context, current DB (namespace) id and the server socket path. More...
 

Macros

#define _GNU_SOURCE
 
#define G_LOG_DOMAIN   "lib kb"
 
#define GLOBAL_DBINDEX_NAME   "GVM.__GlobalDBIndex"
 Name of the namespace usage bitmap in redis. More...
 
#define KB_RETRY_DELAY   60
 Number of seconds to wait for between two attempts to acquire a KB namespace. More...
 
#define redis_kb(__kb)   ((struct kb_redis *) (__kb))
 
#define MAX_DB_INDEX__24   1000
 Max number of configured DB. More...
 

Functions

static int redis_delete_all (struct kb_redis *kbr)
 Delete all the KB's content. More...
 
static int redis_lnk_reset (kb_t kb)
 Reset connection to the KB. This is called after each fork() to make sure connections aren't shared between concurrent processes. More...
 
static int redis_flush_all (kb_t kb, const char *except)
 Flush all the KB's content. Delete all namespaces. More...
 
static redisReply * redis_cmd (struct kb_redis *kbr, const char *fmt,...)
 Execute a redis command and get a redis reply. More...
 
static int try_database_index (struct kb_redis *kbr, int index)
 Attempt to atomically acquire ownership of a database. More...
 
static int fetch_max_db_index_compat (struct kb_redis *kbr)
 Set the number of databases have been configured into kbr struct. (For Redis 2.4.* compatibility). More...
 
static int fetch_max_db_index (struct kb_redis *kbr)
 Set the number of databases have been configured into kbr struct. More...
 
static int select_database (struct kb_redis *kbr)
 Select DB. More...
 
static int redis_release_db (struct kb_redis *kbr)
 Release DB. More...
 
static redisContext * get_redis_ctx (struct kb_redis *kbr)
 Get redis context if it is already connected or do a a connection. More...
 
static int redis_test_connection (struct kb_redis *kbr)
 Test redis connection. More...
 
static int redis_delete (kb_t kb)
 Delete all entries and release ownership on the namespace. More...
 
static int redis_get_kb_index (kb_t kb)
 Return the kb index. More...
 
static int redis_new (kb_t *kb, const char *kb_path)
 Initialize a new Knowledge Base object. More...
 
static kb_t redis_direct_conn (const char *kb_path, const int kb_index)
 Connect to a Knowledge Base object with the given kb_index. More...
 
static kb_t redis_find (const char *kb_path, const char *key)
 Find an existing Knowledge Base object with key. More...
 
void kb_item_free (struct kb_item *item)
 Release a KB item (or a list). More...
 
static struct kb_itemredis2kbitem_single (const char *name, const redisReply *elt, int force_int)
 Give a single KB item. More...
 
static struct kb_itemredis2kbitem (const char *name, const redisReply *rep)
 Fetch a KB item or list from a redis Reply. More...
 
static struct kb_itemredis_get_single (kb_t kb, const char *name, enum kb_item_type type)
 Get a single KB element. More...
 
static char * redis_get_str (kb_t kb, const char *name)
 Get a single KB string item. More...
 
static int redis_push_str (kb_t kb, const char *name, const char *value)
 Push a new entry under a given key. More...
 
static char * redis_pop_str (kb_t kb, const char *name)
 Pops a single KB string item. More...
 
static int redis_get_int (kb_t kb, const char *name)
 Get a single KB integer item. More...
 
static char * redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position)
 Get field of a NVT. More...
 
static nvti_tredis_get_nvt_all (kb_t kb, const char *oid)
 Get a full NVT. More...
 
static struct kb_itemredis_get_all (kb_t kb, const char *name)
 Get all items stored under a given name. More...
 
static struct kb_itemredis_get_pattern (kb_t kb, const char *pattern)
 Get all items stored under a given pattern. More...
 
static GSList * redis_get_oids (kb_t kb)
 Get all NVT OIDs. More...
 
static size_t redis_count (kb_t kb, const char *pattern)
 Count all items stored under a given pattern. More...
 
static int redis_del_items (kb_t kb, const char *name)
 Delete all entries under a given name. More...
 
static int redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
 Insert (append) a new unique entry under a given name. More...
 
static int redis_add_str (kb_t kb, const char *name, const char *str, size_t len)
 Insert (append) a new entry under a given name. More...
 
static int redis_set_str (kb_t kb, const char *name, const char *val, size_t len)
 Set (replace) a new entry under a given name. More...
 
static int redis_add_int_unique (kb_t kb, const char *name, int val)
 Insert (append) a new unique entry under a given name. More...
 
static int redis_add_int (kb_t kb, const char *name, int val)
 Insert (append) a new entry under a given name. More...
 
static int redis_set_int (kb_t kb, const char *name, int val)
 Set (replace) a new entry under a given name. More...
 
static int redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename)
 Insert a new nvt. More...
 
int redis_save (kb_t kb)
 Save all the elements from the KB. More...
 

Variables

static const struct kb_operations KBRedisOperations
 Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based). More...
 
const struct kb_operationsKBDefaultOperations = &KBRedisOperations
 Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based). More...
 

Detailed Description

Knowledge base management API - Redis backend.

Contains specialized structures and functions to use redis as a KB server.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib kb"

◆ GLOBAL_DBINDEX_NAME

#define GLOBAL_DBINDEX_NAME   "GVM.__GlobalDBIndex"

Name of the namespace usage bitmap in redis.

◆ KB_RETRY_DELAY

#define KB_RETRY_DELAY   60

Number of seconds to wait for between two attempts to acquire a KB namespace.

◆ MAX_DB_INDEX__24

#define MAX_DB_INDEX__24   1000

Max number of configured DB.

◆ redis_kb

#define redis_kb (   __kb)    ((struct kb_redis *) (__kb))

Function Documentation

◆ fetch_max_db_index()

static int fetch_max_db_index ( struct kb_redis kbr)
static

Set the number of databases have been configured into kbr struct.

Parameters
[in]kbrSubclass of struct kb where to save the max db index founded.
Returns
0 on success, -1 on error.

◆ fetch_max_db_index_compat()

static int fetch_max_db_index_compat ( struct kb_redis kbr)
static

Set the number of databases have been configured into kbr struct. (For Redis 2.4.* compatibility).

Parameters
[in]kbrSubclass of struct kb where to save the max db index founded.
Returns
0 on success, -1 on error.

◆ get_redis_ctx()

static redisContext* get_redis_ctx ( struct kb_redis kbr)
static

Get redis context if it is already connected or do a a connection.

Parameters
[in]kbrSubclass of struct kb where to fetch the context. or where it is saved in case of a new connection.
Returns
Redis context on success, NULL otherwise.

◆ kb_item_free()

void kb_item_free ( struct kb_item item)

Release a KB item (or a list).

Parameters
[in]itemItem or list to be release

◆ redis2kbitem()

static struct kb_item* redis2kbitem ( const char *  name,
const redisReply *  rep 
)
static

Fetch a KB item or list from a redis Reply.

Parameters
[in]nameName of the item.
[in]repA redisReply element where to fetch the item.
Returns
kb_item or list on success, NULL otherwise.

◆ redis2kbitem_single()

static struct kb_item* redis2kbitem_single ( const char *  name,
const redisReply *  elt,
int  force_int 
)
static

Give a single KB item.

Parameters
[in]nameName of the item.
[in]eltA redisReply element where to fetch the item.
[in]force_intTo force string to integer conversion.
Returns
Single retrieve kb_item on success, NULL otherwise.

◆ redis_add_int()

static int redis_add_int ( kb_t  kb,
const char *  name,
int  val 
)
static

Insert (append) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
Returns
0 on success, non-null on error.

◆ redis_add_int_unique()

static int redis_add_int_unique ( kb_t  kb,
const char *  name,
int  val 
)
static

Insert (append) a new unique entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
Returns
0 on success, non-null on error.

◆ redis_add_nvt()

static int redis_add_nvt ( kb_t  kb,
const nvti_t nvt,
const char *  filename 
)
static

Insert a new nvt.

Parameters
[in]kbKB handle where to store the nvt.
[in]nvtnvt to store.
[in]filenamePath to nvt to store.
Returns
0 on success, non-null on error.

◆ redis_add_str()

static int redis_add_str ( kb_t  kb,
const char *  name,
const char *  str,
size_t  len 
)
static

Insert (append) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]strItem value.
[in]lenValue length. Used for blobs.
Returns
0 on success, non-null on error.

◆ redis_add_str_unique()

static int redis_add_str_unique ( kb_t  kb,
const char *  name,
const char *  str,
size_t  len 
)
static

Insert (append) a new unique entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]strItem value.
[in]lenValue length. Used for blobs.
Returns
0 on success, non-null on error.

◆ redis_cmd()

static redisReply * redis_cmd ( struct kb_redis kbr,
const char *  fmt,
  ... 
)
static

Execute a redis command and get a redis reply.

Parameters
[in]kbrSubclass of struct kb to connect to.
[in]fmtFormatted variable argument list with the cmd to be executed.
Returns
Redis reply on success, NULL otherwise.

◆ redis_count()

static size_t redis_count ( kb_t  kb,
const char *  pattern 
)
static

Count all items stored under a given pattern.

Parameters
[in]kbKB handle where to count the items.
[in]pattern'*' pattern of the elements to count.
Returns
Count of items.

◆ redis_del_items()

static int redis_del_items ( kb_t  kb,
const char *  name 
)
static

Delete all entries under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
Returns
0 on success, non-null on error.

◆ redis_delete()

static int redis_delete ( kb_t  kb)
static

Delete all entries and release ownership on the namespace.

Parameters
[in]kbKB handle to release.
Returns
0 on success, non-null on error.

◆ redis_delete_all()

int redis_delete_all ( struct kb_redis kbr)
static

Delete all the KB's content.

Parameters
[in]kbrSubclass of struct kb.
Returns
0 on success, non-null on error.

◆ redis_direct_conn()

static kb_t redis_direct_conn ( const char *  kb_path,
const int  kb_index 
)
static

Connect to a Knowledge Base object with the given kb_index.

Parameters
[in]kb_pathPath to KB.
[in]kb_indexDB index
Returns
Knowledge Base object, NULL otherwise.

◆ redis_find()

static kb_t redis_find ( const char *  kb_path,
const char *  key 
)
static

Find an existing Knowledge Base object with key.

Parameters
[in]kb_pathPath to KB.
[in]keyMarker key to search for in KB objects.
Returns
Knowledge Base object, NULL otherwise.

◆ redis_flush_all()

static int redis_flush_all ( kb_t  kb,
const char *  except 
)
static

Flush all the KB's content. Delete all namespaces.

Parameters
[in]kbKB handle.
[in]exceptDon't flush DB with except key.
Returns
0 on success, non-null on error.

◆ redis_get_all()

static struct kb_item* redis_get_all ( kb_t  kb,
const char *  name 
)
static

Get all items stored under a given name.

Parameters
[in]kbKB handle where to fetch the items.
[in]nameName of the elements to retrieve.
Returns
Linked struct kb_item instances to be freed with kb_item_free() or NULL if no element was found or on error.

◆ redis_get_int()

static int redis_get_int ( kb_t  kb,
const char *  name 
)
static

Get a single KB integer item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
Returns
A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

◆ redis_get_kb_index()

static int redis_get_kb_index ( kb_t  kb)
static

Return the kb index.

Parameters
[in]kbKB handle.
Returns
kb_index on success, null on error.

◆ redis_get_nvt()

static char* redis_get_nvt ( kb_t  kb,
const char *  oid,
enum kb_nvt_pos  position 
)
static

Get field of a NVT.

Parameters
[in]kbKB handle where to store the nvt.
[in]oidOID of NVT to get from.
[in]positionPosition of field to get.
Returns
Value of field, NULL otherwise.

◆ redis_get_nvt_all()

static nvti_t* redis_get_nvt_all ( kb_t  kb,
const char *  oid 
)
static

Get a full NVT.

Parameters
[in]kbKB handle where to store the nvt.
[in]oidOID of NVT to get.
Returns
nvti_t of NVT, NULL otherwise.

◆ redis_get_oids()

static GSList* redis_get_oids ( kb_t  kb)
static

Get all NVT OIDs.

Parameters
[in]kbKB handle where to fetch the items.
Returns
Linked list of all OIDs or NULL.

◆ redis_get_pattern()

static struct kb_item* redis_get_pattern ( kb_t  kb,
const char *  pattern 
)
static

Get all items stored under a given pattern.

Parameters
[in]kbKB handle where to fetch the items.
[in]pattern'*' pattern of the elements to retrieve.
Returns
Linked struct kb_item instances to be freed with kb_item_free() or NULL if no element was found or on error.

◆ redis_get_single()

static struct kb_item* redis_get_single ( kb_t  kb,
const char *  name,
enum kb_item_type  type 
)
static

Get a single KB element.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
[in]typeDesired element type.
Returns
A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

◆ redis_get_str()

static char* redis_get_str ( kb_t  kb,
const char *  name 
)
static

Get a single KB string item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
Returns
A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

◆ redis_lnk_reset()

static int redis_lnk_reset ( kb_t  kb)
static

Reset connection to the KB. This is called after each fork() to make sure connections aren't shared between concurrent processes.

Parameters
[in]kbKB handle.
Returns
0 on success, non-null on error.

◆ redis_new()

static int redis_new ( kb_t kb,
const char *  kb_path 
)
static

Initialize a new Knowledge Base object.

Parameters
[in]kbReference to a kb_t to initialize.
[in]kb_pathPath to KB.
Returns
0 on success, non-null on error.

◆ redis_pop_str()

static char* redis_pop_str ( kb_t  kb,
const char *  name 
)
static

Pops a single KB string item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the key from where to retrieve.
Returns
A string to be freed or NULL if list is empty or on error.

◆ redis_push_str()

static int redis_push_str ( kb_t  kb,
const char *  name,
const char *  value 
)
static

Push a new entry under a given key.

Parameters
[in]kbKB handle where to store the item.
[in]nameKey to push to.
[in]valueValue to push.
Returns
0 on success, non-null on error.

◆ redis_release_db()

static int redis_release_db ( struct kb_redis kbr)
static

Release DB.

Parameters
[in]kbrSubclass of struct kb.
Returns
0 on success, -1 on error.

◆ redis_save()

int redis_save ( kb_t  kb)

Save all the elements from the KB.

Parameters
[in]kbKB handle.
Returns
0 on success, -1 on error.

◆ redis_set_int()

static int redis_set_int ( kb_t  kb,
const char *  name,
int  val 
)
static

Set (replace) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
Returns
0 on success, non-null on error.

◆ redis_set_str()

static int redis_set_str ( kb_t  kb,
const char *  name,
const char *  val,
size_t  len 
)
static

Set (replace) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
[in]lenValue length. Used for blobs.
Returns
0 on success, non-null on error.

◆ redis_test_connection()

static int redis_test_connection ( struct kb_redis kbr)
static

Test redis connection.

Parameters
[in]kbrSubclass of struct kb to test.
Returns
0 on success, negative integer on error.

◆ select_database()

static int select_database ( struct kb_redis kbr)
static

Select DB.

Parameters
[in]kbrSubclass of struct kb where to save the db index.
Returns
0 on success, -1 on error.

WARNING: do not call redis_cmd in here, since our context is not fully acquired yet!

◆ try_database_index()

static int try_database_index ( struct kb_redis kbr,
int  index 
)
static

Attempt to atomically acquire ownership of a database.

Returns
0 on success, negative integer otherwise.

Variable Documentation

◆ KBDefaultOperations

const struct kb_operations* KBDefaultOperations = &KBRedisOperations

Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).

◆ KBRedisOperations

static const struct kb_operations KBRedisOperations
static
Initial value:
= {
.kb_new = redis_new,
.kb_find = redis_find,
.kb_delete = redis_delete,
.kb_get_single = redis_get_single,
.kb_get_str = redis_get_str,
.kb_get_int = redis_get_int,
.kb_get_nvt = redis_get_nvt,
.kb_get_nvt_all = redis_get_nvt_all,
.kb_get_nvt_oids = redis_get_oids,
.kb_push_str = redis_push_str,
.kb_pop_str = redis_pop_str,
.kb_get_all = redis_get_all,
.kb_get_pattern = redis_get_pattern,
.kb_count = redis_count,
.kb_add_str = redis_add_str,
.kb_add_str_unique = redis_add_str_unique,
.kb_set_str = redis_set_str,
.kb_add_int = redis_add_int,
.kb_add_int_unique = redis_add_int_unique,
.kb_set_int = redis_set_int,
.kb_add_nvt = redis_add_nvt,
.kb_del_items = redis_del_items,
.kb_lnk_reset = redis_lnk_reset,
.kb_save = redis_save,
.kb_flush = redis_flush_all,
.kb_direct_conn = redis_direct_conn,
.kb_get_kb_index = redis_get_kb_index,
}
static int redis_push_str(kb_t kb, const char *name, const char *value)
Push a new entry under a given key.
Definition: kb.c:832
static int redis_add_str(kb_t kb, const char *name, const char *str, size_t len)
Insert (append) a new entry under a given name.
Definition: kb.c:1218
static nvti_t * redis_get_nvt_all(kb_t kb, const char *oid)
Get a full NVT.
Definition: kb.c:936
static struct kb_item * redis_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition: kb.c:774
static char * redis_pop_str(kb_t kb, const char *name)
Pops a single KB string item.
Definition: kb.c:856
static struct kb_item * redis_get_all(kb_t kb, const char *name)
Get all items stored under a given name.
Definition: kb.c:985
static int redis_add_nvt(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition: kb.c:1372
static GSList * redis_get_oids(kb_t kb)
Get all NVT OIDs.
Definition: kb.c:1074
static int redis_flush_all(kb_t, const char *)
Flush all the KB's content. Delete all namespaces.
Definition: kb.c:1449
static int redis_set_int(kb_t kb, const char *name, int val)
Set (replace) a new entry under a given name.
Definition: kb.c:1341
static int redis_lnk_reset(kb_t)
Reset connection to the KB. This is called after each fork() to make sure connections aren't shared b...
Definition: kb.c:1427
static kb_t redis_direct_conn(const char *kb_path, const int kb_index)
Connect to a Knowledge Base object with the given kb_index.
Definition: kb.c:498
static int redis_add_str_unique(kb_t kb, const char *name, const char *str, size_t len)
Insert (append) a new unique entry under a given name.
Definition: kb.c:1166
static size_t redis_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition: kb.c:1109
static int redis_add_int(kb_t kb, const char *name, int val)
Insert (append) a new entry under a given name.
Definition: kb.c:1319
static char * redis_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.c:807
static int redis_set_str(kb_t kb, const char *name, const char *val, size_t len)
Set (replace) a new entry under a given name.
Definition: kb.c:1246
static int redis_add_int_unique(kb_t kb, const char *name, int val)
Insert (append) a new unique entry under a given name.
Definition: kb.c:1282
int redis_save(kb_t kb)
Save all the elements from the KB.
Definition: kb.c:1525
static int redis_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition: kb.c:427
static char * redis_get_nvt(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition: kb.c:906
static kb_t redis_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition: kb.c:538
static int redis_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition: kb.c:468
static int redis_get_int(kb_t kb, const char *name)
Get a single KB integer item.
Definition: kb.c:882
static int redis_get_kb_index(kb_t kb)
Return the kb index.
Definition: kb.c:452
static struct kb_item * redis_get_pattern(kb_t kb, const char *pattern)
Get all items stored under a given pattern.
Definition: kb.c:1012
static int redis_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition: kb.c:1139

Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).